『QQ:1353814576』

C# HttpWebRequest POST 简单例子


C# HttpWebRequest 简单例子

string URL="http://www.yuantk.com";
string PostStr="一个不专业的.NET开发者博客";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "post";//post方式
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
byte[] buffer = encoding.GetBytes(PostStr);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using( StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
{
           return reader.ReadToEnd();//返回请求得到的字符串
}