C# POST提交请求 并获得返回值 可用于接口
时间:2011-03-30 来源:轻典
string strURL = "http://211.136.105.27:81/test.aspx";
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 = "id=10";
byte[] payload;
//将URL编码后的字符串转化为字节
payload = System.Text.Encoding.UTF32.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";
}
Response.Write(strValue);