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

开心网辅助程序开发手记

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

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

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

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

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

由于之前有写过类似的程序,也写过相关的文章介绍过(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
    }
}

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

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

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

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)

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:单元测试相关。目前源码只是一个类库

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

点击下载源码

转载于:https://www.cnblogs.com/jailu/archive/2008/12/06/1349313.html

开心网辅助程序开发手记相关推荐

  1. 微信小程序开发手记1.0

    腾讯小程序还是很屌的,最近一波小游戏把小程序推到了风口,我司也抓住机会想来试一试水.于是我开发了一个红包小程序,从学习架构语法接口到最后完成第一版,用时一个月.遇到了无数的坎坷,无数摇头叹气想骂人.所 ...

  2. 微信跳一跳辅助程序开发,基于C++与opencv图像识别

    趁着期末这段时间,课程不多,在学习opencv,闲来无事,看到网上有大神用python实现了Wechat的跳一跳的辅助外挂,看了大概原理,似乎跟我最近学的opencv好像很沾边,但是鄙人实在不懂Pyt ...

  3. 微信小程序开发手记之七:一个小程序上线后的总结(上)

    终于,经过大概一周时间,磕磕绊绊地提交审核了,一个移动猿开发小程序,也算有了些心得,也遇到了些坑,这里和大家一起分享下. 怎么样调布局 先说一个题外话,最后引入正题. 如果翻看过一些资料,大家肯定很容 ...

  4. insight 后台性能监控小程序开发手记

    项目概述 项目源码 开发工具使用 原型设计 编码调试 项目自动化构建 API 管理 后台支持 版本控制与管理 开发存在问题与解决方案 Echarts 使用问题 自定义导航栏 + 默认 tabBar 出 ...

  5. 微信小程序开发手记之三:backgroud和border属性

    先来看一段样式,在wxss中 page{background-color: cadetblue;background-image: url(../../../image/weixin_logo.png ...

  6. 微信小程序开发优秀教程及文章合集第一期

    2019独角兽企业重金招聘Python工程师标准>>> 我会不定期的选取一些优质教程,整理成辑,以便大家集中阅读: 新手向!微信小程序开发手记系列: 微信小程序开发手记<一&g ...

  7. 微信小程序开发导航:精品教程+网友观点+demo源码(5月9日更新)

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  8. c语言跳一跳辅助源码,.NET 开发一个微信跳一跳辅助程序(附源码)

    原标题:.NET 开发一个微信跳一跳辅助程序(附源码) 来源:中国.NET研究协会 cnblogs.com/dotnet-org-cn/p/8149693.html 前言 微信更新了,出现了一个小游戏 ...

  9. 推荐程序开发辅助软件(流程图软件+代码注释软件)

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.流程图软件单机版draw.io 二.代码注释软件AAcircuit 后面不定期更新 前言 提示:这里可以添加本文要 ...

最新文章

  1. android studio 使用CMAKE
  2. 自顶向下的语法分析(修改)
  3. (转)博弈 SG函数
  4. Android SDK tools,platform-tools,build-tools 区别
  5. 初,中,高级的 ABAPer 应该各自具备什么水准的开发能力
  6. cdockpane限制调整大小_影视后期制作小伙伴必看:使用AU对声音质量进行调整的三大技巧...
  7. 如何将Jupyter Notebook连接到远程Spark集群并每天运行Spark作业?
  8. C语言入门经验:零基础如何学习C语言?
  9. Ubuntu 15 周年!
  10. npm ERR! command failednpm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
  11. 基于Docker的开发模式驱动持续集成落地实施
  12. 利用PYTHON计算偏相关系数(Partial correlation coefficient)
  13. 利用Power BI制作分级地图报表
  14. 微信支付——委托代扣介绍
  15. 优矿python开源_PythonStock(8):使用优矿web学习python入门
  16. 加扣扣群所有脚本免费使用
  17. USB 发展 缺陷 与 未来
  18. 无业务不技术:那些誓用区块链重塑的行业,发展怎么样了?
  19. 一些Pixel手机的使用技巧
  20. abap bdc附加选项

热门文章

  1. BRAC模型 权限表设计
  2. 开发外贸客户邮箱,怎么精准开发外贸客户邮箱?
  3. 获取历史和实时股票数据接口
  4. 如何查看raid控制器的信息HP DELL
  5. 从vivo Photo Lab“影像实验室”透视门店新价值
  6. 计算机毕业设计springboot交通事故档案管理平台ryug8源码+系统+程序+lw文档+部署
  7. NOIP历年第二轮入门组真题集合
  8. iphone6 计算机无法检测到照相机,苹果iPhone XR摄像头黑屏不能照相是什么原因?...
  9. javascript关于累加和的发散思维
  10. Java实现10万+并发去重,持续优化!(至尊典藏版)