程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 使用HttpWebrequest對網站進行模擬操作(附登陸百度demo),httpwebrequestdemo

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

編輯:C#入門知識

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


 


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


 

不出意外 以後將在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);
                stream.Write(headerBytes, 0, headerBytes.Length);

                //寫入文件
                byte[] fileBytes = File.ReadAllBytes(filepath);
                stream.Write(fileBytes, 0, fileBytes.Length);

                //寫入尾部
                byte[] footerBytes = Encoding.Default.GetBytes("\r\n--" + boundary + "--\r\n");
                stream.Write(footerBytes, 0, footerBytes.Length);

                using (HttpWebResponse hrp = hrq.GetResponse() as HttpWebResponse)//響應流
                {
                    using (StreamReader SR = new StreamReader(hrp.GetResponseStream(), encode))
                    {
                        return SR.ReadToEnd();
                    }
                }
            }

        }



    }

 

 


 

登陸百度示例:

登陸百度的一個demo,我在13年寫的 =。= ,現在登陸加密了,不過以前的這種方式仍然可以用,呵呵

 

吐槽一下,從來沒寫過博客,無論是新浪,QQ,還是其它。今天才發現這排版太累。

 

 

 之後打算寫一下,模擬webqq請求來實現QQ機器人。盡量寫成一個系列吧...麼麼哒(*゚∀゚*)

 

 

.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved