admin
2020-06-17 9d3d08ba960fc739498b0648d57eaf2c50a40fd1
WindowsFormsApp1/utils/HttpUtil.cs
@@ -11,6 +11,7 @@
{
    class HttpUtil
    {
        static CookieContainer cookieContainer = new CookieContainer();
        //Get网络请求
        public static string HttpGet(String url, Dictionary<String,String> headerMap) {
            string retString = string.Empty;
@@ -39,11 +40,31 @@
            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";
@@ -63,7 +84,7 @@
                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("&"))
@@ -82,5 +103,17 @@
            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());
        }
    }
}