using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; namespace WindowsFormsApp1.utils { class HttpUtil { static CookieContainer cookieContainer = new CookieContainer(); //Get网络请求 public static string HttpGet(String url, Dictionary headerMap) { string retString = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Timeout = 60000; //设置代理 WebProxy proxy = new WebProxy(); proxy.Address = new Uri("http://192.168.1.122:8888"); request.Proxy = proxy; WebHeaderCollection headers = request.Headers; if (headerMap != null) { foreach (KeyValuePair kvp in headerMap) { headers.Add(kvp.Key + ":" + kvp.Value); } } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(myResponseStream); retString = streamReader.ReadToEnd(); streamReader.Close(); myResponseStream.Close(); return retString; } public static Stream HttpGetImage(String url) { string retString = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cookieContainer; request.Method = "GET"; request.Timeout = 60000; //设置代理 WebProxy proxy = new WebProxy(); proxy.Address = new Uri("http://192.168.1.122:8888"); request.Proxy = proxy; WebHeaderCollection headers = request.Headers; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); return myResponseStream; } //POST网络请求 public static string HttpPost(String url, Dictionary paramsMap,Dictionary headerMap) { string retString = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cookieContainer; request.Method = "POST"; request.Timeout = 60000; request.ContentType = "application/x-www-form-urlencoded"; //设置代理 WebProxy proxy = new WebProxy(); proxy.Address = new Uri("http://192.168.1.122:8888"); request.Proxy = proxy; WebHeaderCollection headers = request.Headers; if (headerMap != null) { foreach (KeyValuePair kvp in headerMap) { headers.Add(kvp.Key + ":" + kvp.Value); } } if (paramsMap != null) { StringBuilder buffer = new StringBuilder(); foreach (KeyValuePair kvp in paramsMap) { buffer.AppendFormat("&{0}={1}", kvp.Key, UrlEncode(kvp.Value)); } if (buffer.ToString().StartsWith("&")) buffer.Remove(0,1); Encoding encoding = Encoding.UTF8; byte[] postData = encoding.GetBytes(buffer.ToString()); request.ContentLength = postData.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(postData, 0, postData.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(myResponseStream); retString = streamReader.ReadToEnd(); streamReader.Close(); myResponseStream.Close(); return retString; } public static string UrlEncode(string str){ return HttpUtility.UrlEncode(str); // StringBuilder sb = new StringBuilder(); //byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //for (int i = 0; i < byStr.Length; i++) //{ // sb.Append(@"%" + Convert.ToString(byStr[i], 16)); // } // return (sb.ToString()); } } }