91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

post提交獲得html頁面源碼的案例

發布時間:2020-10-26 09:29:38 來源:億速云 閱讀:314 作者:小新 欄目:web開發

小編給大家分享一下post提交獲得html頁面源碼的案例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

/// <summary>        /// 獲得頁面的html源碼  主要用于后臺生成靜態文件時獲得源碼        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static string GetPageHTML(string url)
        {
            string httpString = string.Empty;
            WebRequest request = WebRequest.Create(url);
            request.Timeout = 200000;            
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusDescription.ToLower().Equals("ok"))
                {
                    using (StreamReader writer = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")))
                    {
                        httpString = writer.ReadToEnd();
                    }
                }
            }
            return httpString;
        }
 
 
        /// <summary>        /// 獲得頁面的html源碼  主要用于后臺生成靜態文件時獲得源碼UTF-8        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static string GetPageHTMLUTF8(string url)
        {
            string httpString = string.Empty;
            WebRequest request = WebRequest.Create(url);
            request.Timeout = 200000;            
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusDescription.ToLower().Equals("ok"))
                {
                    using (StreamReader writer = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
                    {
                        httpString = writer.ReadToEnd();
                    }
                }
            }
            return httpString;
        }
 
 
        /// <summary>        /// post提交JSON數據。支持.net4.0及以下的版本        /// </summary>        /// <param name="url"></param>        /// <param name="json"></param>        /// <returns></returns>        public static string GetHtmlByJson(string url, string json = "")
        {
            var result = string.Empty;
 
            try            {
                var request = WebRequest.Create(url) as HttpWebRequest;
                request.ContentType = "text/json";
                request.Method = "post";
                //request.CookieContainer = _cookie;
                 using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
 
                    var response = (HttpWebResponse)request.GetResponse();
 
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            catch (UriFormatException uex)
            {
                // 出錯處理            }
 
            return result;
        }
 
        /// <summary>         /// 利用WebClient 遠程POST數據并返回數據         /// </summary>         /// <param name="strUrl">遠程URL地址</param>         /// <param name="strParams">參數,要提交的JSON字符串</param>         /// <param name="RespEncode">POST數據的編碼</param>         /// <param name="ReqEncode">獲取數據的編碼</param>         /// <returns></returns>         public static string PostData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
        {
            /**             * 本函數只支持.net4.5以上的框架            HttpClient httpclient = new HttpClient();            try            {                //打開頁面                 httpclient.Credentials = CredentialCache.DefaultCredentials;                //從指定的URI下載資源                 byte[] responseData = httpclient.DownloadData(strUrl);                string srcString = RespEncode.GetString(responseData);
                 httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");                string postString = strParams;                // 將字符串轉換成字節數組                 byte[] postData = Encoding.ASCII.GetBytes(postString);                // 上傳數據,返回頁面的字節數組                 responseData = httpclient.UploadData(strUrl, "POST", postData);                srcString = ReqEncode.GetString(responseData);
                 return srcString;            }            catch (Exception ex)            {                //記錄異常日志                 //釋放資源                 httpclient.Dispose();                return string.Empty;            }            */            return "";
        }
 
 
        /// <summary>        /// 執行POST提交范例        /// </summary>        /// <param name="url"></param>        /// <param name="postdata">"LoginName=365admin&Password=fob123"</param>        /// <returns></returns>        public static string PostPageHTMLUTF8(string url, string postdata)
        {
            WebClient client = new WebClient();
            System.Collections.Specialized.NameValueCollection list = new System.Collections.Specialized.NameValueCollection();            
            list.Add("opencheckindatatype", "3");
            list.Add("starttime", "1492617600");
            list.Add("endtime", "1492790400");
            // "useridlist": ["james","paul"]                          list.Add("useridlist", "['TuHuaXing']");            
            byte[] j = client.UploadValues(url, list);
            //var dec = BitConverter.ToInt64(j,0);            //string jS = BitConverter.ToString(j);            //jS = Convert.ToString(jS,10);            return System.Text.Encoding.Default.GetString(j);
 
            //目標頁面獲取值   request.form["id"]
             /**            WebRequest request7 = WebRequest.Create(url);            request7.Method = "POST";
             //post傳參數                        byte[] bytes = Encoding.ASCII.GetBytes(postdata);            request7.ContentType = "application/x-www-form-urlencoded";            request7.ContentLength = postdata.Length;            request7.S            Stream sendStream = request7.GetRequestStream();            sendStream.Write(bytes, 0, bytes.Length);            sendStream.Close();
             //得到返回值              WebResponse response7 = request7.GetResponse();            string OrderQuantity = new StreamReader(response7.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();            return OrderQuantity;            //轉化成json對象處理            //List<GetOrderQuantity> getOrderQuantity = sr.Deserialize<List<GetOrderQuantity>>(OrderQuantity);            **/        }

看完了這篇文章,相信你對post提交獲得html頁面源碼的案例有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

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

AI

牟定县| 项城市| 长子县| 凤台县| 陆良县| 兴化市| 友谊县| 平阴县| 长宁区| 涡阳县| 林西县| 佳木斯市| 韩城市| 武汉市| 东海县| 平谷区| 西青区| 临桂县| 武川县| 沅陵县| 北川| 徐州市| 保靖县| 盐边县| 黑河市| 商丘市| 白玉县| 台北市| 阿克| 依安县| 宁强县| 曲周县| 方正县| 民和| 疏勒县| 马龙县| 河源市| 贞丰县| 金坛市| 安福县| 沂水县|