C# Http POST get
using System.IO;
using System.Net;
/// <summary>
/// HttpWebRequest发送Post请求
/// </summary>
/// <param name="postUrl"></param>
/// <param name="paramData"></param>
/// <param name="dataEncode"></param>
/// <returns></returns>
public static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
{
string ret = string.Empty;
byte[] byteArray = dataEncode.GetBytes(paramData);
//转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
return ret; }
/// <summary>
/// WebClient发送Get请求,编码UTF8
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public static string Get(string Url) {
string result = "";
HttpWebRequest httpReq;
httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url));
WebResponse wr = httpReq.GetResponse();
Stream responseStream = wr.GetResponseStream();
StreamReader respStreamReader = new StreamReader(responseStream);
result = respStreamReader.ReadToEnd();
responseStream.Dispose(); wr.Dispose();
return result;
}