HttpWebRequest发送post请求时有多个参数如何处理
1个回答
展开全部
private void button1_Click(object sender, EventArgs e)
{
//测试编码问题
string p1 = "测试的代码<啊啊啊>";
string p2 = "\"'===&&";
string p3 = "??";
string strURL = "http://localhost:2852/WebSite1/Service1.asmx/doSearch";
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
// 内容类型
request.ContentType = "application/x-www-form-urlencoded";
//这是原始代码:
string paraUrlCoded = "p1=" + Uri.EscapeDataString(p1) + "&p2=" + Uri.EscapeDataString(p2) + "&p3=" + Uri.EscapeDataString(p3);
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();
XmlDocument d = new XmlDocument();
d.Load(s);
MessageBox.Show(d.DocumentElement.InnerText);
}
{
//测试编码问题
string p1 = "测试的代码<啊啊啊>";
string p2 = "\"'===&&";
string p3 = "??";
string strURL = "http://localhost:2852/WebSite1/Service1.asmx/doSearch";
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
// 内容类型
request.ContentType = "application/x-www-form-urlencoded";
//这是原始代码:
string paraUrlCoded = "p1=" + Uri.EscapeDataString(p1) + "&p2=" + Uri.EscapeDataString(p2) + "&p3=" + Uri.EscapeDataString(p3);
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();
XmlDocument d = new XmlDocument();
d.Load(s);
MessageBox.Show(d.DocumentElement.InnerText);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询