public class WebRequestHelper
{
/// <summary>
/// 以POST 形式请求数据
/// </summary>
/// <param name="RequestPara"></param>
/// <param name="Url"></param>
/// <returns></returns>
public static string PostData(string RequestPara,string Url)
{
WebRequest hr = HttpWebRequest.Create(Url);
byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
hr.ContentType = "application/x-www-form-urlencoded";
hr.ContentLength = buf.Length;
hr.Method = "POST";
System.IO.Stream RequestStream = hr.GetRequestStream();
RequestStream.Write(buf, 0, buf.Length);
RequestStream.Close();
System.Net.WebResponse response = hr.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
string ReturnVal = reader.ReadToEnd();
reader.Close();
response.Close();
return ReturnVal;
}
/// <summary>
/// 以GET 形式获取数据
/// </summary>
/// <param name="RequestPara"></param>
/// <param name="Url"></param>
/// <returns></returns>
public static string GetData(string RequestPara, string Url)
{
RequestPara=RequestPara.IndexOf('?')>-1?(RequestPara):("?"+RequestPara);
WebRequest hr = HttpWebRequest.Create(Url + RequestPara);
byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
hr.Method = "GET";
System.Net.WebResponse response = hr.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
string ReturnVal = reader.ReadToEnd();
reader.Close();
response.Close();
return ReturnVal;
}
}
|