C#模擬webget請(qǐng)求、post請(qǐng)求-創(chuàng)新互聯(lián)

get請(qǐng)求:

成都創(chuàng)新互聯(lián)長(zhǎng)期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為尼瑪企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作,尼瑪網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
#region  get請(qǐng)求
        /// <summary>
        /// get請(qǐng)求
        /// </summary>
        /// <param name="Url">請(qǐng)求地址</param>
        /// <param name="postDataStr">請(qǐng)求參數(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請(qǐng)求:

#region  模擬Post提交
        /// <summary>
        /// 通過POST方式發(fā)送數(shù)據(jù)
        /// </summary>
        /// <param name="url">請(qǐng)求URL</param>
        /// <param name="json">請(qǐng)求參數(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

詳解請(qǐng)參考:http://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。

文章標(biāo)題:C#模擬webget請(qǐng)求、post請(qǐng)求-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://www.muchs.cn/article6/cshhog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、小程序開發(fā)、云服務(wù)器面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管