開心網輔助程式開發手記

來源:互聯網
上載者:User

聲明:本人只在業餘空閑時間寫寫《開心網輔助程式》,目的只是學習!

由於之前有寫過類似的程式,也寫過相關的文章介紹過(C#網站登入學習筆記(一):登入簡單網站、C#網站登入學習筆記(二):訪問需登入後才能訪問的頁面),這次寫起“開心網輔助程式”也可以算是得心應手了,直接從電腦中翻出塵封已久的HttpHelper(前面提到的兩篇文章就是居於這個操作類進行的),稍微分析了一下網頁結構(爭車位),就寫起程式來了!

在開始寫手記前,讓我們看看寫這樣的“外掛”程式需要準備什麼軟體?

1. 抓包工具:Http Analyzer V3。既然要實現的是Http類比請求,抓包工具肯定少不了了

2. 網頁分析工具:Firefox 3.0 + Firebug 1.2.1。沒錯,可愛的Firefox狸又來幫忙了

在這篇手記中,將簡單的介紹一下如何登入開心網、擷取爭車位相關資料。

一、稍微修改了一下HttpHelper類的代碼:

Code

using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;  namespace SNSHelper.Common{  class HttpHelper  {    #region 私人變數    private CookieContainer cc;    private string contentType = "application/x-www-form-urlencoded";    private string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";    private string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";    private Encoding encoding = Encoding.GetEncoding("utf-8");    #endregion      #region 屬性    /// <summary>    /// Cookie容器    /// </summary>    public CookieContainer CookieContainer    {      get      {        return cc;      }    }      /// <summary>    /// 擷取網頁源碼時使用的編碼    /// </summary>    /// <value></value>    public Encoding Encoding    {      get      {        return encoding;      }      set      {        encoding = value;      }    }    #endregion      #region 建構函式    /// <summary>    /// Initializes a new instance of the <see cref="HttpHelper"/> class.    /// </summary>    public HttpHelper()    {      cc = new CookieContainer();    }      /// <summary>    /// Initializes a new instance of the <see cref="HttpHelper"/> class.    /// </summary>    /// <param name="cc">The cc.</param>    public HttpHelper(CookieContainer cc)    {      this.cc = cc;    }      /// <summary>    /// Initializes a new instance of the <see cref="HttpHelper"/> class.    /// </summary>    /// <param name="contentType">Type of the content.</param>    /// <param name="accept">The accept.</param>    /// <param name="userAgent">The user agent.</param>    public HttpHelper(string contentType, string accept, string userAgent)    {      this.contentType = contentType;      this.accept = accept;      this.userAgent = userAgent;    }      /// <summary>    /// Initializes a new instance of the <see cref="HttpHelper"/> class.    /// </summary>    /// <param name="cc">The cc.</param>    /// <param name="contentType">Type of the content.</param>    /// <param name="accept">The accept.</param>    /// <param name="userAgent">The user agent.</param>    public HttpHelper(CookieContainer cc, string contentType, string accept, string userAgent)    {      this.cc = cc;      this.contentType = contentType;      this.accept = accept;      this.userAgent = userAgent;    }    #endregion      #region 公用方法    /// <summary>    /// 擷取指定頁面的HTML代碼    /// </summary>    /// <param name="url">指定頁面的路徑</param>    /// <param name="postData">回傳的資料</param>    /// <param name="isPost">是否以post方式發送請求</param>    /// <param name="cookieCollection">Cookie集合</param>    /// <returns></returns>    public string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)    {      if (string.IsNullOrEmpty(postData))      {        return GetHtml(url, cookieContainer);      }        byte[] byteRequest = Encoding.Default.GetBytes(postData);        HttpWebRequest httpWebRequest;      httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);        httpWebRequest.CookieContainer = cookieContainer;      httpWebRequest.ContentType = contentType;      httpWebRequest.Referer = url;      httpWebRequest.Accept = accept;      httpWebRequest.UserAgent = userAgent;      httpWebRequest.Method = isPost ? "POST" : "GET";      httpWebRequest.ContentLength = byteRequest.Length;        Stream stream = httpWebRequest.GetRequestStream();      stream.Write(byteRequest, 0, byteRequest.Length);      stream.Close();        HttpWebResponse httpWebResponse;      httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();      Stream responseStream = httpWebResponse.GetResponseStream();      StreamReader streamReader = new StreamReader(responseStream, encoding);      string html = streamReader.ReadToEnd();      streamReader.Close();      responseStream.Close();        return html;    }      /// <summary>    /// 擷取指定頁面的HTML代碼    /// </summary>    /// <param name="url">指定頁面的路徑</param>    /// <param name="cookieCollection">Cookie集合</param>    /// <returns></returns>    public string GetHtml(string url, CookieContainer cookieContainer)    {      HttpWebRequest httpWebRequest;        httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);      httpWebRequest.CookieContainer = cookieContainer;      httpWebRequest.ContentType = contentType;      httpWebRequest.Referer = url;      httpWebRequest.Accept = accept;      httpWebRequest.UserAgent = userAgent;      httpWebRequest.Method = "GET";        HttpWebResponse httpWebResponse;      httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();      Stream responseStream = httpWebResponse.GetResponseStream();      StreamReader streamReader = new StreamReader(responseStream, encoding);      string html = streamReader.ReadToEnd();      streamReader.Close();      responseStream.Close();        return html;    }    /// <summary>    /// 擷取指定頁面的HTML代碼    /// </summary>    /// <param name="url">指定頁面的路徑</param>    /// <returns></returns>    public string GetHtml(string url)    {      return GetHtml(url, cc);    }      /// <summary>    /// 擷取指定頁面的HTML代碼    /// </summary>    /// <param name="url">指定頁面的路徑</param>    /// <param name="postData">回傳的資料</param>    /// <param name="isPost">是否以post方式發送請求</param>    /// <returns></returns>    public string GetHtml(string url, string postData, bool isPost)    {      return GetHtml(url, postData, isPost, cc);    }    #endregion  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.