Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选

首页 / 网页编程 / ASP.NET / 开心网辅助程序开发手记

开心网辅助程序开发手记2011-12-13 博客园 jailu声明:本人只在业余空闲时间写写《开心网辅助程序》,目的只是学习!

由于之前有写过类似的程序,也写过相关的文章介绍过(C#网站登录学习笔记(一):登录简单网站、C#网站登录学习笔记(二):访问需登录后才能访问的页面),这次写起“开心网辅助程序”也可以算是得心应手了,直接从电脑中翻出尘封已久的HttpHelper(前面提到的两篇文章就是居于这个操作类进行的),稍微分析了一下网页结构(争车位),就写起程序来了!

在开始写手记前,让我们看看写这样的“外挂”程序需要准备什么软件?

1. 抓包工具:Http Analyzer V3。既然要实现的是Http模拟请求,抓包工具肯定少不了了

2. 网页分析工具:Firefox 3.0 + Firebug 1.2.1。没错,可爱的火狐狸又来帮忙了

在这篇手记中,将简单的介绍一下如何登录开心网、获取争车位相关数据。

一、稍微修改了一下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  }}