=波波日志 > Asp.Net/C#/WCF > 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();
类别:Asp.Net/C#/WCF 作者:波波 日期:2011-11-01 【评论:0】 
 
暂时没有评论!
发表留言
  • *昵称:
  • 头像:
  • 电子邮件: [留下您的邮件,方便管理员回复您。]
  • 个人网站: *验证码:
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,©2012,桂ICP备05005887号 京公网安备1101055090