http請求工具類

  


#region MD5加密方法

        /// <summary>
        /// MD5加密方法
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static String AKMD5(String s)
        {




            char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };  //這個串可以自定義
            try
            {
                byte[] btInput = System.Text.Encoding.Default.GetBytes(s);
                // 獲得MD5摘要算法的 MessageDigest 對象
                MD5 mdInst = System.Security.Cryptography.MD5.Create();
                // 使用指定的字節更新摘要
                mdInst.ComputeHash(btInput);
                // 獲得密文
                byte[] md = mdInst.Hash;
                // 把密文轉換成十六進制的字符串形式
                int j = md.Length;
                char[] str = new char[j * 2];
                int k = 0;
                for (int i = 0; i < j; i++)
                {
                    byte byte0 = md[i];
                    str[k++] = hexDigits[(int)(((byte)byte0) >> 4) & 0xf];
                    str[k++] = hexDigits[byte0 & 0xf];
                }
                return new string(str);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.StackTrace);
                return null;
            }
        }


        #endregion


        #region 獲取Json字符串某節點的值
        /// <summary>
        /// 獲取Json字符串某節點的值
        /// </summary>
        public static string GetJsonValue(string jsonStr, string key)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(jsonStr))
            {
                key = "\"" + key.Trim('"') + "\"";
                int index = jsonStr.IndexOf(key) + key.Length + 1;
                if (index > key.Length + 1)
                {
                    //先截逗號,若是最後一個,截“}”號,取最小值
                    int end = jsonStr.IndexOf(',', index);
                    if (end == -1)
                    {
                        end = jsonStr.IndexOf('}', index);
                    }


                    result = jsonStr.Substring(index, end - index);
                    result = result.Trim(new char[] { '"', ' ', '\'' }); //過濾引號或空格
                }
            }
            return result;
        }
        #endregion


        #region 請求Url
        #region 請求Url,不發送數據
        /// <summary>
        /// 請求Url,不發送數據
        /// </summary>
        public static string RequestUrl(string url)
        {
            return RequestUrl(url, "POST");
        }


        public static string GetUrl(string url)
        {
            return RequestUrl(url, "GET");
        }
        #endregion


        #region 請求Url,不發送數據
        /// <summary>
        /// 請求Url,不發送數據
        /// </summary>
        public static string RequestUrl(string url, string method)
        {
            // 設置參數
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = method;
            request.ContentType = "text/html";
            request.Headers.Add("charset", "utf-8");
            //發送請求並獲取相應迴應數據
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序纔開始向目標網頁發送Post請求
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
            //返回結果網頁(html)代碼
            string content = sr.ReadToEnd();
            return content;
        }
        #endregion


        #region 請求Url,發送數據
        /// <summary>
        /// 請求Url,發送數據
        /// </summary>
        public static string PostUrl(string url, string postData)
        {
            byte[] data = Encoding.UTF8.GetBytes(postData);


            // 設置參數
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            Stream outstream = request.GetRequestStream();
            outstream.Write(data, 0, data.Length);
            outstream.Close();


            //發送請求並獲取相應迴應數據
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序纔開始向目標網頁發送Post請求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回結果網頁(html)代碼
            string content = sr.ReadToEnd();
            return content;
        }
        #endregion
        #endregion


        #region  生成 CDATA封裝的xml


        /// <summary>
        /// 判斷是否是整數
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumeric(String str)
        {
            try
            {
                int.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 生成 CDATA封裝的xml
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static string ArrayToXml(Dictionary<string, string> arr)
        {
            String xml = "<info>";


            foreach (KeyValuePair<string, string> pair in arr)
            {
                String key = pair.Key;
                String val = pair.Value;
                xml += "<" + key + "><![CDATA[" + val + "]]></" + key + ">";
            }


            xml += "</info>";
            return xml;
        }




        #endregion
發佈了47 篇原創文章 · 獲贊 160 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章