溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C# 模擬web   get請求、post請求

發(fā)布時間:2020-07-05 04:20:36 來源:網(wǎng)絡 閱讀:2081 作者:lovejuan1314 欄目:編程語言

get請求:

#region  get請求
        /// <summary>
        /// get請求
        /// </summary>
        /// <param name="Url">請求地址</param>
        /// <param name="postDataStr">請求參數(shù)</param>
        /// <returns></returns>
        public static string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            #region 獲取網(wǎng)頁內(nèi)容太大的話,就加下面這兩句代碼
            request.Headers["Accept-Encoding"] = "gzip,deflate"; 
            request.AutomaticDecompression = DecompressionMethods.GZip;
            #endregion
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        #endregion

post請求:

#region  模擬Post提交
        /// <summary>
        /// 通過POST方式發(fā)送數(shù)據(jù)
        /// </summary>
        /// <param name="url">請求URL</param>
        /// <param name="json">請求參數(shù)</param>
        /// <returns></returns>
        public static string HttpPost(string url, string strXML)
        {
            try
            {
                Encoding encoding = Encoding.GetEncoding("UTF-8");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
                byte[] buffer;
                buffer = encoding.GetBytes(strXML);
                request.Method = "Post";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER";
                request.ContentType = "application/json; charset=UTF-8";//application/x-www-form-urlencoded;charset=UTF-8
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,p_w_picpath/webp,*/*;q=0.8";
                request.Referer = "http://www.aramex.com/express/track-results.aspx";
                request.ContentLength = buffer.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(buffer, 0, buffer.Length);
                postStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                //返回信息
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                string strResponse = reader.ReadToEnd();

                reader.Close();
                response.Close();
                return strResponse;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion


詳解請參考:http://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

bs st
AI