C#抓取遠(yuǎn)程頁面內(nèi)容,以POST|GET方式抓取遠(yuǎn)程頁面內(nèi)容
以GET方式抓取遠(yuǎn)程頁面內(nèi)容
以POST方式抓取遠(yuǎn)程頁面內(nèi)容
using System; using System.Data; using System.Web.UI; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions;//包含必要的庫 namespace Utils { /// <summary> /// 抓取遠(yuǎn)程頁面內(nèi)容 /// </summary> public static class HttpHelp { //以GET方式抓取遠(yuǎn)程頁面內(nèi)容 public static string Get_Http(string tUrl) { string strResult; try { HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(tUrl); hwr.Timeout = 19600; HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); Stream myStream = hwrs.GetResponseStream(); StreamReader sr = new StreamReader(myStream, Encoding.Default); StringBuilder sb = new StringBuilder(); while (-1 != sr.Peek()) { sb.Append(sr.ReadLine() "\r\n"); } strResult = sb.ToString(); hwrs.Close(); } catch (Exception ee) { strResult = ee.Message; } return strResult; } //以POST方式抓取遠(yuǎn)程頁面內(nèi)容 //postData為參數(shù)列表 public static string Post_Http(string url, string postData, string encodeType) { string strResult = null; try { Encoding encoding = Encoding.GetEncoding(encodeType); byte[] POST = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = POST.Length; Stream newStream = myRequest.GetRequestStream(); newStream.Write(POST, 0, POST.Length); //設(shè)置POST newStream.Close(); // 獲取結(jié)果數(shù)據(jù) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); strResult = reader.ReadToEnd(); } catch (Exception ex) { strResult = ex.Message; } return strResult; } } }
原文鏈接:C#原生實現(xiàn)POST|GET方式發(fā)送Http請求幫助類