| | |
| | | { |
| | | class HttpUtil |
| | | { |
| | | static CookieContainer cookieContainer = new CookieContainer(); |
| | | //Get网络请求 |
| | | public static string HttpGet(String url, Dictionary<String,String> headerMap) { |
| | | string retString = string.Empty; |
| | |
| | | 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<String, String> paramsMap,Dictionary<String, String> 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"; |
| | |
| | | StringBuilder buffer = new StringBuilder(); |
| | | foreach (KeyValuePair<string, string> kvp in paramsMap) |
| | | { |
| | | buffer.AppendFormat("&{0}={1}", kvp.Key, kvp.Value); |
| | | buffer.AppendFormat("&{0}={1}", kvp.Key, UrlEncode(kvp.Value)); |
| | | } |
| | | |
| | | if (buffer.ToString().StartsWith("&")) |
| | |
| | | myResponseStream.Close(); |
| | | return retString; |
| | | } |
| | | |
| | | |
| | | public static string UrlEncode(string 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()); |
| | | } |
| | | |
| | | } |
| | | } |