using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; public class WebHTTPUtil { public CookieContainer CookieContainer { get; set; } public CookieCollection CookieCollection { get; set; } public WebRequest Request{ get; set; } public WebHTTPUtil() { this.CookieCollection = new CookieCollection(); this.CookieContainer = new CookieContainer(); } /// <summary> /// 以POST 形式请求数据 /// </summary> /// <param name="RequestPara"></param> /// <param name="Url"></param> /// <returns></returns> public string PostData(string Url,string RequestPara) { System.GC.Collect (); Request = HttpWebRequest.Create(Url); RequestPara=Regex.Replace(RequestPara,"%", "%25"); byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara); Request.ContentType = "application/x-www-form-urlencoded"; Request.ContentLength = buf.Length; Request.Method = "POST"; System.IO.Stream RequestStream = Request.GetRequestStream(); RequestStream.Write(buf, 0, buf.Length); RequestStream.Close(); System.Net.WebResponse response = Request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); string ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); Request = null; return ReturnVal; } /// <summary> /// 以GET 形式获取数据 /// </summary> /// <param name="RequestPara"></param> /// <param name="Url"></param> /// <returns></returns> public string GetData(string Url, string RequestPara) { System.GC.Collect (); RequestPara=RequestPara.IndexOf('?')>-1?(RequestPara):("?"+RequestPara); RequestPara=Regex.Replace(RequestPara,"%", "%25"); Request = HttpWebRequest.Create(Url + RequestPara); byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara); Request.Method = "GET"; System.Net.WebResponse response = Request.GetResponse(); string status = ((HttpWebResponse)response).StatusDescription; if (!status.Equals ("OK")) { response.Close(); return "ERROR"; } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); string ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); Request = null; return ReturnVal; } }