HttpWebRequest发送post请求时有多个参数如何处理?
在网上找了一段winform中发送post请求的代码,但是我要post的参数有多个,我试了下用&,|连接都出错。报远程服务器返回错误:(400)错误的请求。
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch";
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method="POST";
// 内容类型
request.ContentType="application/x-www-form-urlencoded";
// 参数经过URL编码
//这是原始代码:
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text);
//这是我的代码:
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("userCode");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text);
paraUrlCoded ="&"+ System.Web.HttpUtility.UrlEncode("userName");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox2.Text);
byte[] payload;
//将URL编码后的字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的 ContentLength
request.ContentLength = payload.Length;
//获得请 求流
Stream writer = request.GetRequestStream();
//将请求参数写入流
writer.Write(payload,0,payload.Length);
// 关闭请求流
writer.Close();
System.Net.HttpWebResponse response;
// 获得响应流
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<","<");
strValue = strValue.Replace(">",">");
MessageBox.Show(strValue);
Reader.Close();
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch";
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method="POST";
// 内容类型
request.ContentType="application/x-www-form-urlencoded";
// 参数经过URL编码
//这是原始代码:
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text);
//这是我的代码:
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("userCode");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text);
paraUrlCoded ="&"+ System.Web.HttpUtility.UrlEncode("userName");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox2.Text);
byte[] payload;
//将URL编码后的字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的 ContentLength
request.ContentLength = payload.Length;
//获得请 求流
Stream writer = request.GetRequestStream();
//将请求参数写入流
writer.Write(payload,0,payload.Length);
// 关闭请求流
writer.Close();
System.Net.HttpWebResponse response;
// 获得响应流
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<","<");
strValue = strValue.Replace(">",">");
MessageBox.Show(strValue);
Reader.Close();
...全文
相关推荐
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
C#模拟http 发送post或get请求的简单实例
下面小编就为大家带来一篇C#模拟http 发送post或get请求的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
C#(Csharp)多线程HTTP并发请求(HttpWebRequest)采集蜘蛛
C#(也称Csharp)在多线程下并发执行HTTP请求的实现,采用C#封装HttpWebRequest类开发的多线程并发采集程序源码文档,文档中详细说明了HttpWebRequest并发HTTP请求实现网站采集的方法,经过测试同时并发1000+不是问题,好东西和大家分享哦:)
使用HttpWebRequest发送自定义POST请求
使用 HttpWebRequest发送自定义POST请求
C#中HttpWebRequest的用法详解
主要介绍了C#中HttpWebRequest的用法,以实例的形式详细叙述了HttpWebRequest类中GET与POST的用法,非常具有参考借鉴价值,需要的朋友可以参考下


