HttpWebRequest发送post请求时有多个参数如何处理

2024-11-20 22:43:27
推荐回答(1个)
回答1:

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=x&p2=y&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);