问题描述
- http怎么传输一个很长的字符串
-
winform端向webform传一个很长的字符串,超过get的长度限制,应该怎么写
我用的是webclient 实在不知道怎么写,uploadstring好像不行 还是我用法不对 希望能大神解惑
然后服务器怎么接收这个字符串 ,希望能有代码
解决方案
转成流StreamReader,还有分长度的吗?传文件都是字节流,大小由IIS决定的,HTTP就这些吧
解决方案二:
使用post
方法........
解决方案三:
get方式,长度不能超过1024或2048个字节。
有两个方式:用post方式,或用http压缩传送
解决方案四:
public static string SendMsg(string fxPhone, string fxPassword, string toPhone, string msg)
{
try
{
string url = "u=" + fxPhone + "&";
url = url + "p=";
url = url + fxPassword + "&";
url = url + "to=";
url = url + toPhone + "&";
url = url + "m=" + msg;
string formUrl = "http://quanapi.sinaapp.com/fetion.php";
string formData = url; //提交的参数
//注意提交的编码 这边是需要改变的 这边默认的是Default:系统当前编码
byte[] postData = Encoding.UTF8.GetBytes(formData);
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.UTF8;
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
HttpWebResponse response;
Stream responseStream;
StreamReader reader;
string srcString;
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
srcString = reader.ReadToEnd();
string result = srcString; //返回值赋值
reader.Close();
return result;
}
catch
{
return "error";
}
}
解决方案五:
如果你要使用webclient,如下:
public static void PostString (string address)
{
string data = "Time = 12:00am temperature = 50";
string method = "POST";
WebClient client = new WebClient ();
string reply = client.UploadString (address, method, data);
Console.WriteLine (reply);
}
解决方案六:
如果你使用asp.net webform,直接用httpRequest的form来取,你自己写以下,调一调就懂了。
HTTP GET/POST是在content-type中指定的,你可以顺便了解下HTTP协议。
如果你使用mvc,可以参考以下代码:
//
// POST: /Customer/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
时间: 2024-11-18 12:46:13