使用 HttpWebRequest模擬登錄(附帶驗證碼)

在C#中,能夠使用HttpWebRequest進行相關的模擬登錄,登錄後進行相關的操做,好比抓取數據,頁面分析,製做相關登錄助手等等。web

先說下流程c#

1.使用httpwebrequest先進入你要登陸的網站,獲取cookie服務器

2.使用第一步獲取的cookie到驗證碼的網頁將驗證碼下載下來。cookie

3.使用Post數據 發送至網站。若是有cookie則繼續保存。app

4.使用第三步的cookie登錄相關網頁操做。ide


獲取相關數據能夠使用抓包工具進行抓取,如httpwatch。(網上下載的好多都有病毒,下載的時候注意點)工具

1。post

/// <summary>
/// 經過get方式請求頁面,傳遞一個實例化的cookieContainer
/// </summary>
/// <param name="postUrl"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
{
    HttpWebRequest request;
    HttpWebResponse response;
    ArrayList list = new ArrayList();
    request = WebRequest.Create(postUrl) as HttpWebRequest;
    request.Method = "GET";
    request.UserAgent = "Mozilla/4.0";
    request.CookieContainer = cookie;
    request.KeepAlive = true;
    request.CookieContainer = cookie;
    try
    {
        //獲取服務器返回的資源
        using (response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
            {
                cookie.Add(response.Cookies);
                //保存Cookies
                list.Add(cookie);
                list.Add(reader.ReadToEnd());
                list.Add(Guid.NewGuid().ToString());//圖片名
            }
        }
    }
    catch (WebException ex)
    {
        list.Clear();
        list.Add("發生異常/n/r");
        WebResponse wr = ex.Response;
        using (Stream st = wr.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
            {
                list.Add(sr.ReadToEnd());
            }
        }
    }
    catch (Exception ex)
    {
        list.Clear();
        list.Add("5");
        list.Add("發生異常:" + ex.Message);
    }
    return list;
}


2.下載驗證碼,保存在本地。網站

/// <summary>
        /// 下載驗證碼圖片並保存到本地
        /// </summary>
        /// <param name="Url">驗證碼URL</param>
        /// <param name="cookCon">Cookies值</param>
        /// <param name="savePath">保存位置/文件名</param>
        public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
        {
            bool bol = true;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
            //屬性配置
            webRequest.AllowWriteStreamBuffering = true;
            webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            webRequest.MaximumResponseHeadersLength = -1;
            webRequest.Accept = "p_w_picpath/gif, p_w_picpath/x-xbitmap, p_w_picpath/jpeg, p_w_picpath/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "GET";
            webRequest.Headers.Add("Accept-Language", "zh-cn");
            webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
            webRequest.KeepAlive = true;
            webRequest.CookieContainer = cookCon;
            try
            {
                //獲取服務器返回的資源
                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (Stream sream = webResponse.GetResponseStream())
                    {
                        List<byte> list = new List<byte>();
                        while (true)
                        {
                            int data = sream.ReadByte();
                            if (data == -1)
                                break;
                            list.Add((byte)data);
                        }
                        File.WriteAllBytes(savePath, list.ToArray());
                    }
                }
            }
            catch (WebException ex)
            {
                bol = false;
            }
            catch (Exception ex)
            {
                bol = false;
            }
            return bol;
        }


3。發送post數據ui

/// <summary>
/// 發送相關數據至頁面
/// 進行登陸操做
/// 並保存cookie
/// </summary>
/// <param name="postData"></param>
/// <param name="postUrl"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
{
    ArrayList list = new ArrayList();
    HttpWebRequest request;
    HttpWebResponse response;
    ASCIIEncoding encoding = new ASCIIEncoding();
    request = WebRequest.Create(postUrl) as HttpWebRequest;
    byte[] b = encoding.GetBytes(postData);
    request.UserAgent = "Mozilla/4.0";
    request.Method = "POST";
    request.CookieContainer = cookie;
    request.ContentLength = b.Length;
    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(b, 0, b.Length);
    }
    try
    {
        //獲取服務器返回的資源
        using (response = request.GetResponse() as HttpWebResponse)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                if (response.Cookies.Count > 0)
                    cookie.Add(response.Cookies);
                list.Add(cookie);
                list.Add(reader.ReadToEnd());
            }
        }
    }
    catch (WebException wex)
    {
        WebResponse wr = wex.Response;
        using (Stream st = wr.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
            {
                list.Add(sr.ReadToEnd());
            }
        }
    }
    catch (Exception ex)
    {
        list.Add("發生異常/n/r"+ex.Message);
    }
    return list;
}

4。就是第三步請求的連接地址換一個就好了


好了

以上核心代碼已經貼出了

具體實現須要靠大家按照大家本身的邏輯


還有一些header能不寫就不寫,由於我2天前一直在獲取返回response這地方報500錯誤。

找了N多代碼,看了N多資料都不能夠。最後將一些header註釋掉就能夠了,真鬱悶。

相關文章
相關標籤/搜索