string strURL = "http://www.cqjg.gov.cn/newwww/c7/clwz.asp";
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("Name");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());
paraUrlCoded +="& "+ System.Web.HttpUtility.UrlEncode("Age");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarNumber.Text.Trim());
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();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s,Encoding.Default);
while ((StrDate= Reader.ReadLine())!=null)
{
strValue += StrDate+"\r\n";
}
// txtInfo.Text = strValue;
txtInfo.Text= FindContent(strValue);
如果浏览器请求的 正文是 Name=小明那么
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("Name");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());
就能成功
如果是Name=小明&type=1
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("Name");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());
paraUrlCoded +="& "+ System.Web.HttpUtility.UrlEncode("type");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(1);
这样就会失败提交不成功
还有问下如果请求主题是{"Item":{"Name":"小明"}}这样子或者是{"Name":"test"}这样子的话我该怎么传呢? 麻烦请教下了