Asp.net的post提交方式

//建立WebRequest对象,url目标地址
HttpWebRequest req =(HttpWebRequest)WebRequest.Create(url);

//将LoginInfo转换为byte[]格式,这里的LoginInfo应该是你要传输的数据
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(LoginInfo);

//设置请求为POST方式,
req.Method = "POST";

//设置请求类型
req.ContentType = "application/x-www-form-urlencoded";

//请求发送的数据长度, 这里就很清楚了,请求数据是从LoginInfo转换来的
//前面的代码byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(LoginInfo);
req.ContentLength = requestBytes.Length;

//建立请求的输入流
Stream requestStream = req.GetRequestStream();

//从requestBytes中读取数据到输入流中
requestStream.Write(requestBytes, 0, requestBytes.Length);

//关闭输入流
requestStream.Close();

//获取响应对象
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

//获取服务器返回流
StreamReader sr = new StreamReader(res.GetResponseStream(),System.Text.Encoding.Default);

//读取返回流数据,并赋值给backstr
string backstr =sr.ReadToEnd();

//页面输出backstr
Response.Write(backstr);
//关闭* 
sr.Close();
res.Close();

-------------------------------------------------------------------------------
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";
            }
        }
View Code

-------------------------------------------------------------------------------------
FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型,默认的缺省值是“application/x-www-form-urlencoded”。

然而,在向服务器发送大量的文本、包含非ASCII字符的文本或二进制数据时这种编码方式效率很低。

在文件上载时,所使用的编码类型应当是“multipart/form-data”,它既可以发送文本数据,也支持二进制数据上载。

Browser端<form>表单的ENCTYPE属性值为multipart/form-data,它告诉我们传输的数据要用到多媒体传输协议,由于多媒体传输的都是大量的数据,所以规定上传文件必须是post方法,<input>的type属性必须是file。

----------------------------------------------------------------------------------------------------------

application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。

multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。

text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。


说明:
      form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。

      当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。

当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。

如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。

-----------------------------------------------------------------------------------------------------------

 
原文地址:https://www.cnblogs.com/ecollab/p/6117477.html