现在的位置: 首页 > 综合 > 正文

Java实现网易163邮箱好友通讯录的解析功能(带源码)

2012年12月02日 ⁄ 综合 ⁄ 共 4087字 ⁄ 字号 评论关闭

这个源码 我之前开源过,昨晚又做了一下。公开源码。这里讲下思路 以及真实源码实现。我们将使用1个类HttpClient,这个类的基本用法可以参照:

http://www.ibm.com/developerworks/cn/opensource/os-httpclient/

我们会使用httpClient以及Httpwatch工具。

watch可以在http://www.ij2ee.com/what-to-use-to-develop 有下载地址。


正式开始:

首先 我们用我们的账户密码登录 这里是thieftest 密码a123456  为了他人方便,请不要修改密码

为了更快的找到对应的请求地址,我们直接搜索我们的用户名

我们获取了 https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?df=webmail163&from=web&funcid=loginone&iframe=1&language=-1&net=t&passtype=1&product=mail163&race=234_62_188_db&style=-1&uid=thieftest@163.com

这么一串字符串 ,我们可以看到它是一个POST请求,所以我们需要用httpclient的POST请求来请求服务器。 下面是POST和GET请求的核心代码

 

public static String doGet(HttpClient client, String url, String charCode)
		throws URISyntaxException, IllegalStateException, IOException,
		HttpException, InterruptedException {
	HttpGet get = new HttpGet(url);
	return StringUtil.readInputStream(client.execute(get).getEntity()
			.getContent(), charCode);
}

public static String doPost(HttpClient client, String url,
		Map<String, String> param, String charCode)
		throws URISyntaxException, IllegalStateException, IOException,
		HttpException, InterruptedException {

	NameValuePair nvps[] = new BasicNameValuePair[param.size()];
	int i = 0;
	for (Map.Entry<String, String> entry : param.entrySet()) {
		NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry
				.getValue());
		nvps[i++] = nvp;
	}
	HttpPost httpPost = new HttpPost(url);

	httpPost.setEntity(new UrlEncodedFormEntity(nvps, charCode));
	HttpResponse response = client.execute(httpPost);
	if(response.getStatusLine().getStatusCode()!=200){
		throw new RuntimeException("网页抓取失败,HTTP CODE:"+response.getStatusLine().getStatusCode());
	}
	InputStream is = response.getEntity().getContent();
	return StringUtil.readInputStream(is, charCode);
}

  

根据返回的信息我们分析成功与否,判断的方式就是看有没得到一个叫sid的参数

返回成功的话 会在消息里有 index?sid=xxxxxxxxx 这一段。 这里的xxxxxxx是至关重要的,我们需要获取他。 这里我们可以使用正则表达式。来获取 大概代码如下:

private static String regex = "iframe src=\"index.jsp\\?sid=([^\"]+)";
public static String getByRegex(String regex, int index, String txt) {
	Pattern p = Pattern.compile(regex,Pattern.DOTALL);
	Matcher m = p.matcher(txt);
	if (m.find()) {
		return m.group(index);
	}
	return null;
}

点通讯后 我们抓包发现了一个URL 貌似记录都在里面。

是JSON的撒。要是数据多了 我们可以在 www.bejson.com 上查看目录结构

但是我们请求这个URL后 发现它就返回了

<?xml version="1.0" encoding="UTF-8" ?>
- <result>
  <code>S_OK</code>
  </result>

 看来这条路不通啊。继续找把。忽然发现还有打印的操作能得到所有的我们想要的资料。

String getUsers="http://tg4a84.mail.163.com/jy3/address/addrprint.jsp?sid=前面获取的ID";

请求后得到如下内容

<div class="ContentWp add_print2"><div class="ContentThemeWp"><table width="100%" height="35px" border="0" cellpadding="0" cellspacing="0" bgcolor="f0f9fc"><tr><td width="88%"><b>&nbsp;&nbsp;&nbsp;&nbsp;<span style=" font-size:16px">选择打印的项目</span></b>&nbsp;<input type="checkbox" name="phone" value="phone" onclick="fAddressPrintShow(this)">电话/即时通讯ID<input type="checkbox" name="home" value="home" onclick="fAddressPrintShow(this)">家庭资料<input type="checkbox" name="company" value="company" onclick="fAddressPrintShow(this)">单位/公司<input type="checkbox" name="other" value="other" onclick="fAddressPrintShow(this)">其他信息</span></td><td width="12%"><div align="center"><span style="background-color:#f0f9fc; height:35px; padding-top:8px"><input name="button" type="button" class="Btn BtnNml ImpBtn"  onMouseOver="this.className='Btn BtnHv ImpBtn'" onMouseOut="this.className='Btn BtnNml ImpBtn'" onMouseDown="this.className='Btn BtnHv BtnDw ImpBtn'" hidefocus="ture" value="打 印" style=" margin-bottom:8px" onclick="window.print();"/></span></div></td></tr></table><div class="Hr"><hr /></div><div class="gTitleSub"><div align="left"><b class="mTT">三少</b></div><div class="Extra"></div></div><table class="gTable"><tr id="tr_base_0" style=""><th>邮件地址:</th><td>ij2ee@139.com</td></tr><tr id="tr_base_0" style=""><th>移动电话:</th><td></td></tr><tr id="tr_base_0" style=""><th>生日:</th><td></td></tr><tr id="tr_home_2" style="display:none"><th>联系地址:</th><td>China 中国</td></tr><tr id="tr_company_3" style="display:none"><th>公司地址:</th><td>;;;;;;CI</td></tr><tr id="tr_other_4" style="display:none"><th>备注:</th><td>Java技术博客 www.ij2ee.com</td></tr></table><div class="Hr"><hr /></div><div class="gTitle"><div align="center"><span style="color:#999;">网易公司版权所有</span></div></div></div></div>

下面就剩下解析了。

具体的看源代码。

源码下载: http://115.com/file/bejbru4y#thief.rar 包含源码及jar包 下下来就可以跑,在test包里有测试用例

本文来源:http://www.ij2ee.com/49657.html

 

 

抱歉!评论已关闭.