httpwebRequest post 数据
post 数据过去
private string Post(string strURL, string paramString)
{
string pagehtml = string.Empty;
try
{
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
Uri myUri = new Uri(strURL);
byte[] paramBytes = myEncoding.GetBytes(paramString);
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
myWebRequest.ContentType = "application/x-www-form-urlencoded";
myWebRequest.AllowAutoRedirect = true;
myWebRequest.Method = "POST";
myWebRequest.Timeout = 60000;
myWebRequest.ContentLength = paramBytes.Length;
//Send request
Stream requestStream = myWebRequest.GetRequestStream();
requestStream.Write(paramBytes, 0, paramBytes.Length);
requestStream.Close();
//or we can user below
//或者
using (Stream postStream =myWebRequest.GetRequestStream()) {
using (StreamWriter writer = new StreamWriter(postStream)) {
writer.Write(requestPayload);
}
}
//Get response
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myStreamReader = new StreamReader(myWebResponse.GetResponseStream(), myEncoding);
pagehtml = myStreamReader.ReadToEnd();
myStreamReader.Close();
}
catch (Exception ex)
{
return string.Empty;
}
return pagehtml;
}
{
string pagehtml = string.Empty;
try
{
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
Uri myUri = new Uri(strURL);
byte[] paramBytes = myEncoding.GetBytes(paramString);
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
myWebRequest.ContentType = "application/x-www-form-urlencoded";
myWebRequest.AllowAutoRedirect = true;
myWebRequest.Method = "POST";
myWebRequest.Timeout = 60000;
myWebRequest.ContentLength = paramBytes.Length;
//Send request
Stream requestStream = myWebRequest.GetRequestStream();
requestStream.Write(paramBytes, 0, paramBytes.Length);
requestStream.Close();
//or we can user below
//或者
using (Stream postStream =myWebRequest.GetRequestStream()) {
using (StreamWriter writer = new StreamWriter(postStream)) {
writer.Write(requestPayload);
}
}
//Get response
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myStreamReader = new StreamReader(myWebResponse.GetResponseStream(), myEncoding);
pagehtml = myStreamReader.ReadToEnd();
myStreamReader.Close();
}
catch (Exception ex)
{
return string.Empty;
}
return pagehtml;
}
响应post过来的数据
如果是XML
XML = Request["xml"].ToString();
为response 信息添加头
Response.AppendHeader(
"X-XRDS-Location",
new Uri(Request.Url, Response.ApplyAppPathModifier("~/xrds3")).AbsoluteUri);
"X-XRDS-Location",
new Uri(Request.Url, Response.ApplyAppPathModifier("~/xrds3")).AbsoluteUri);