C# WebRequest Post提交数据
C#使用WebRequest如何Post提交数据,示例代码如下
原创文章,转载请注明出处:C# WebRequest Post提交数据
+展开
-C#
string postStr;
byte[] postBin;
HttpWebRequest request;
HttpWebResponse response;
Stream ioStream;
postStr = "username=showbo&pwd=123456";//键值对
postBin = Encoding.GetEncoding(936).GetBytes(postStr);//注意提交到的网站的编码,现在是gb2312的
request = WebRequest.Create("要POST提交到的url") as HttpWebRequest;
request.Method = "post";
request.KeepAlive = false;
request.AllowAutoRedirect = false;//不允许重定向
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBin.Length;
ioStream = request.GetRequestStream();
ioStream.Write(postBin, 0, postBin.Length);
ioStream.Flush();
ioStream.Close();
response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream(), gb2312);
string r = reader.ReadToEnd();
reader.Close();
response.Close();
byte[] postBin;
HttpWebRequest request;
HttpWebResponse response;
Stream ioStream;
postStr = "username=showbo&pwd=123456";//键值对
postBin = Encoding.GetEncoding(936).GetBytes(postStr);//注意提交到的网站的编码,现在是gb2312的
request = WebRequest.Create("要POST提交到的url") as HttpWebRequest;
request.Method = "post";
request.KeepAlive = false;
request.AllowAutoRedirect = false;//不允许重定向
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBin.Length;
ioStream = request.GetRequestStream();
ioStream.Write(postBin, 0, postBin.Length);
ioStream.Flush();
ioStream.Close();
response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream(), gb2312);
string r = reader.ReadToEnd();
reader.Close();
response.Close();
加支付宝好友偷能量挖...
原创文章,转载请注明出处:C# WebRequest Post提交数据