国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用HttpWebrequest對網站進行模擬操作(附登陸百度demo)

2019-11-17 02:37:50
字體:
來源:轉載
供稿:網友

使用HttpWebrequest對網站進行模擬操作(附登陸百度demo)


這篇文章是在博客園正式的第一篇文章。


不出意外 以后將在web的方向發展,前段時間把老早以前做過的webQQ機器人重做了一遍,算是對winform的告別吧,鞏固了C#方面的知識。

本篇主要介紹了我對模擬http請求方式的介紹和理解。(博客的樣式是自己寫的,有木有感覺好看呢(•‾??‾?•)??°)

首先 看一個GET請求

public string GetHtml(string url, string Referer, Encoding encode, bool SaveCookie)        {            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;            req.Method = "GET";            req.CookieContainer = this.CookieContainer;            req.PRoxy = null;            if (!string.IsNullOrEmpty(Referer))                req.Referer = Referer;            using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)            {                if (SaveCookie)                {                    this.CookieCollection = hwr.Cookies;                    this.CookieContainer.GetCookies(req.RequestUri);                }                using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))                {                    return SR.ReadToEnd();                }            }        }

然后 再看POST

 public string PostHtml(string url, string Referer, string data, Encoding encode, bool SaveCookie)        {            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;            req.CookieContainer = this.CookieContainer;            req.ContentType = "application/x-www-form-urlencoded";            req.Method = "POST";            req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0";            req.Proxy = null;            req.ProtocolVersion = HttpVersion.Version10;            if (!string.IsNullOrEmpty(Referer))                req.Referer = Referer;            byte[] mybyte = Encoding.Default.GetBytes(data);            req.ContentLength = mybyte.Length;            using (Stream stream = req.GetRequestStream())            {                stream.Write(mybyte, 0, mybyte.Length);            }            using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)            {                if (SaveCookie)                {                    this.CookieCollection = hwr.Cookies;                    this.CookieContainer.GetCookies(req.RequestUri);                }                using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))                {                    return SR.ReadToEnd();                }            }        }

小結

1.這是我封裝的一個httphelp類中的一部分,get貌似不需要那些像什么UserAgent,ContentType之類的東西

2.POST請求中一般要設置ContentType和UserAgent

3.默認請求是GET

如何 保持Cookie

每次請求之后保存CookieContainer即可,賦給下一次請求的httpwebrequest

區別 HttpWebRequest.CookieContainer和HttpWebResponse.CookieCollection

此處說的是進行一次http請求獲得響應之后

CookieContainer是當前域的所有Cookie

CookieCollection是該次請求相關的所有Cookie

Referer 是什么

即本次請求的來源,從哪個頁面進行http請求的

這里常被服務器用來檢測請求是否合法

proxy?

請求的代理,對此我了解不多,忘有人能告之

在請求之前設置proxy=null,可以讓請求跳過檢查代理,http請求速度立刻上升一個檔次!尤其是以前蹭wifi的時候我深有體會


這里是我封裝的一個http請求輔助類,大家有興趣可以參考一下

public class HttpHelp    {        public CookieContainer CookieContainer { get; set; }        public CookieCollection CookieCollection { get; set; }        public HttpHelp()        {            this.CookieCollection = new CookieCollection();            this.CookieContainer = new CookieContainer();        }        public static string GetHtml(string url, string Referer, Encoding encode)        {            return new HttpHelp().GetHtml(url, Referer, encode, false);        }        public static string PostHtml(string url, string Referer, string data, Encoding encode)        {            return new HttpHelp().PostHtml(url, Referer, data, encode, false);        }        public string GetHtml(string url, string Referer, Encoding encode, bool SaveCookie)        {            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;            req.Method = "GET";            req.CookieContainer = this.CookieContainer;            req.Proxy = null;            if (!string.IsNullOrEmpty(Referer))                req.Referer = Referer;            using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)            {                if (SaveCookie)                {                    this.CookieCollection = hwr.Cookies;                    this.CookieContainer.GetCookies(req.RequestUri);                }                using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))                {                    return SR.ReadToEnd();                }            }        }        public string PostHtml(string url, string Referer, string data, Encoding encode, bool SaveCookie)        {            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;            req.CookieContainer = this.CookieContainer;            req.ContentType = "application/x-www-form-urlencoded";            req.Method = "POST";            req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0";            req.Proxy = null;            req.ProtocolVersion = HttpVersion.Version10;            if (!string.IsNullOrEmpty(Referer))                req.Referer = Referer;            byte[] mybyte = Encoding.Default.GetBytes(data);            req.ContentLength = mybyte.Length;            using (Stream stream = req.GetRequestStream())            {                stream.Write(mybyte, 0, mybyte.Length);            }            using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)            {                if (SaveCookie)                {                    this.CookieCollection = hwr.Cookies;                    this.CookieContainer.GetCookies(req.RequestUri);                }                using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))                {                    return SR.ReadToEnd();                }            }        }        ///        /// 上傳文件        ///        ///上傳地址        ///文件路徑        ///原網頁file控件name        ///請求流中的contentType        ///返回的encoding        ///post參數字典        ///         public static string PostFile(string url, string filepath, string paramName, string contentType, Encoding encode, Dictionary<string, string> dict)        {            HttpWebRequest hrq = WebRequest.Create(url) as HttpWebRequest;            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");            byte[] boundarybytes = System.Text.Encoding.Default.GetBytes("/r/n--" + boundary + "/r/n");            hrq.ContentType = "multipart/form-data; boundary=" + boundary;            hrq.Method = "POST";            using (Stream stream = hrq.GetRequestStream())   //請求流            {                //寫入post參數                string formdataTemplete = "Content-Disposition: form-data; name=/"{0}/"/r/n/r/n{1}";                if (dict != null && dict.Count > 0)                {                    foreach (KeyValuePair<string, string> pair in dict)                    {                        stream.Write(boundarybytes, 0, boundarybytes.Length);                        string formitem = string.Format(formdataTemplete, pair.Key, pair.Value);                        byte[] formitemBytes = Encoding.Default.GetBytes(formitem);                        stream.Write(formitemBytes, 0, formitemBytes.Length);                    }                }                stream.Write(boundarybytes, 0, boundarybytes.Length);                //寫入頭信息                string headerTemplate = "Content-Disposition: form-data; name=/"{0}/"; filename=/"{1}/"/r/nContent-Type: {2}/r/n/r/n";                string header = string.Format(headerTemplate, paramName, Path.GetFileName(filepath), contentType);                byte[] headerBytes = Encoding.UTF8.GetBytes(header);
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永寿县| 晋宁县| 新巴尔虎右旗| 福建省| 论坛| 德州市| 格尔木市| 遂平县| 通化县| 布尔津县| 吉林省| 峡江县| 讷河市| 米泉市| 北海市| 宁波市| 景谷| 西乌| 潜山县| 河西区| 淄博市| 二连浩特市| 泾阳县| 安塞县| 山东省| 轮台县| 讷河市| 开化县| 商水县| 宁乡县| 湖北省| 辽源市| 绥江县| 临沭县| 沈丘县| 陇西县| 当涂县| 通海县| 曲沃县| 民乐县| 呼伦贝尔市|