开心网辅助程序开发手记

《开心网辅助程序开发手记》 系列导读:

开心网辅助程序开发手记

开心网辅助程序开发手记(二):获取好友私家车位信息

开心网辅助程序开发手记(三):实现停车功能 

开心网辅助程序开发手记(四):贴条功能+逻辑停车+简单界面 

开心网辅助程序--开心网争车位助手正式发布(含源码) 

 

  声明:本人只在业余空闲时间写写《开心网辅助程序》,目的只是学习!

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

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

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

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

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

    一、稍微修改了一下HttpHelper类的代码:

 

ContractedBlock.gif ExpandedBlockStart.gif 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
    }
}

 

    二、登录《开心网(http://www.kaixin001.com)》

      首页找到《开心网》的登录页面,本人用的是http://www.kaixin001.com/login/index.php,打开Http Analyzer V3,选中Firefox进程,启动监听。然后再登录页面中登录,打开Http Analyzer查看监听数据,一看吓一跳--诺大的开心网,登录页面居然没做一点验证?!不过这样也好,为我们这些还心眼的提供了方便。

    下面就是简单的登录程序:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
/// <summary>
/// 登录开心网
/// </summary>
/// <param name="loginEmail">Email</param>
/// <param name="loginPassword">密码</param>
/// <returns></returns>
public static bool Login(string loginEmail, string loginPassword)
{
    
string loginUrl = "http://www.kaixin001.com/login/login.php";
    
string postData = string.Format("url=/home/&invisible_mode=0&email={0}&password={1}", loginEmail, loginPassword);
    
string result = httpHelper.GetHtml(loginUrl, postData, true, cookieContainer);

    
return isLogin = result.Contains("我的首页");
}

 

    三、访问“争车位”页面,获取相关数据

      用Http Analyzer监听发现,打开“争车位”页面,“开心网”依旧没给我们出任何难题,只有带上登录时的Cookie就能直接访问到“争车位”页面(http://www.kaixin001.com/app/app.php?aid=1040)

 

ContractedBlock.gif ExpandedBlockStart.gif Code
CookieContainer cookie = Utility.Cookies;
string parkingHTML = new HttpHelper().GetHtml(AppUrl + AppID, cookie);  // AppUrl = "http://www.kaixin001.com/app/app.php?aid="; AppID = "1040";
if (!parkingHTML.Contains("争车位"))
{
    
throw new Exception("读取争车位信息出错");
}

 

      这么轻易就来到“争车位”页面了,赶紧打开网页源码分析一下,咱得赶紧找出“我的汽车”里的汽车对吧?

      打开源码,Ctrl + F,查找“我的汽车”

      嘿,在源码里居然没发现“我的汽车”信息,真是失望!慢着,“我的汽车”不会藏在setMyCar();方法里吧?找找看!返回Firefox,按下F12,请出我们的大侠Firebug,让它帮忙找找。

     能熟练使用Firebug的朋友,相信你一下子就找到数据了吧,那我给其他还没找到的朋友一点提醒:setMyCar()方法在parking-7.js里呢,但是数据却在app.php?aid=1040中的v_userdata变量中呢!

    提示:当选择Script选项卡时,可用鼠标单击红框部位。有什么用?你点了吗?你没点怎么知道呢?

    仔细一看,乖乖,原来是JSON啊,本人之前只了解过,但不清楚如何使用。得,现学吧。以下是本人搜索到的一些资源,供和我一样不了解JSON的朋友参考:

    http://www.json.org/

    http://james.newtonking.com/projects/json-net.aspx

    http://www.ibm.com/developerworks/cn/web/wa-lo-json/?ca=drs-tp3308

    经过简单的了解后,本人选择了Json.NET来操作JSON。PS:Json.NET下载地址:http://www.codeplex.com/Json,.NET Framework 2.0的请下载Json.NET 1.3.1;最新版本的Json.NET 3.5需要.NET Framework 3.5的支持。

   Newtonsoft.Json.JavaScriptConvert.DeserializeObject方法可用来把JSON字符串转成实体,你只需根据JSON的结构,拼出正确的实体就可以简单的使用该方法把JSON字符串转成对应的实体。如何查看JSON的结构呢?由于本人之前为曾使用过JSON,目前只发现在Http Analyzer V3中,Tools/JSON Viewer可用来查看JSON结构(若您有更好的工具,请您推荐给我)

    上图所示的是v_userdata变量中JSON的树状结构。

    通过分析,v_userdata中包含了用户基本信息(user)、用户车位的停车情况(parking)、用户汽车情况(car);v_frienddata中包含了好友信息。

    由于这块的代码量比较庞大,就不在帖子中展示了,有兴趣的朋友可以自行查看本文附带的源码。

上图是源码结构截图,目前只实现了登录开心网、获取争车位相关数据的功能。

HttpHelper.cs:该类封装了访问网页数据的方法

Kaixin001/Entity:该目录下存放的是开发中使用到的实体

Utility.cs:一些常用的方法

ParkingHelper.cs:和“争车位”相关的方法

SNSHelper_UnitTest:单元测试相关。目前源码只是一个类库


由于本人能力有限,贴出程序只是希望能帮到那些对这方面开发有兴趣的朋友。若您对程序有什么看法,欢迎您和我交流!

点击下载源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值