本文主要是介绍关于winfrom程序http post请求多参数问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
string strURL = "http://123.57.5.197:8080/init";System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
request.KeepAlive = false;
// 内容类型
request.ContentType = "application/x-www-form-urlencoded";
// 参数经过URL编码,此处需注意:每个参数名前都需要加为经过url编码的“&“
string paraUrlCoded = "&"+System.Web.HttpUtility.UrlEncode("id");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.tbno_security.Text);
paraUrlCoded += "&" + System.Web.HttpUtility.UrlEncode("mac");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(Global.mac);
byte[] payload;
//将URL编码后的字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的 ContentLength
request.ContentLength = payload.Length;
//获得请 求流
Stream writer = null;
try
{
writer = request.GetRequestStream();
}
catch (Exception e)
{
throw;
}
//将请求参数写入流
writer.Write(payload, 0, payload.Length);
// 关闭请求流
writer.Close();
System.Net.HttpWebResponse response = null;
// 获得响应流
try
{
response = (System.Net.HttpWebResponse)request.GetResponse();
}
catch (Exception e)
{
throw;
}
System.IO.Stream s;
s = response.GetResponseStream();
StreamReader reader = new StreamReader(s);
string strValue = reader.ReadToEnd(); //如果返回数据比较大,可以选择循环读取
MessageBox.Show(strValue);
reader.Close();
这篇关于关于winfrom程序http post请求多参数问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!