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();

加支付宝好友偷能量挖...


原创文章,转载请注明出处:C# WebRequest Post提交数据

评论(1)Web开发网
阅读(214)喜欢(0)Asp.Net/C#/WCF