Edson188
2015-03-04 17:07:31
原精
apache HttpClient 4.3.4自动登录并抓取中国联通网页用户基本信息和账单数据
1.新建一个maven项目httpclient
2.登录中国联通并抓取数据
3.使用Get模拟登录,抓取每月账单数据
中国联通有两种登录方式:
上面两图的区别一个是带验证码,一个是不带验证码, 下面将先解决不带验证码的登录.
这里有两个难点,一是验证码,二uvc码;
验证码,这里将其写到本地,然后人工输入,这个还比较好解决.
uvc码,很重要,这个是在cookie里的,httpclient操作cookie的方法网上找了很久都没有找到,后来看其源码才看到.
package com.httpclient.asm.demo; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.*; import org.apache.http.util.EntityUtils; import javax.swing.*; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; public class LoginChinaUnicomWithCaptcha { /** * 登录并抓取中国联通数据 * 带验证码登录 * @author Edson.di * @date 2015年3月4日 * @version 1.0 * @throws IOException */ public static void main(String args[]) throws Exception { String name = "联通手机号"; String pwd = "手机服务密码"; //生成验证码的链接 String createCaptchaUrl = "http://uac.10010.com/portal/Service/CreateImage"; HttpClient httpClient = new DefaultHttpClient(); //这里可自定义所需要的cookie CookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCookieStore(cookieStore) .build(); //get captcha,获取验证码 HttpGet captchaHttpGet = new HttpGet(createCaptchaUrl); HttpResponse capthcaResponse = httpClient.execute(captchaHttpGet); if (capthcaResponse.getStatusLine().getStatusCode() == 200) { //将验证码写入本地 saveToLocal(capthcaResponse.getEntity(), "chinaunicom.capthca." + System.currentTimeMillis()+".png"); } //手工输入验证码并验证 HttpResponse verifyResponse = null; String capthca = null; String uvc = null; do { //输入验证码,读入键盘输入 //1) // InputStream inputStream = System.in; // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); // System.out.println("请输入验证码:"); // capthca = bufferedReader.readLine(); capthca=JOptionPane.showInputDialog("请输入图片验证码:"); //2) //Scanner scanner = new Scanner(System.in); //capthca = scanner.next(); // http://uac.10010.com/portal/Service/CtaIdyChk?callback=jsonp1404716227598&verifyCode=4m3e&verifyType=1 String verifyCaptchaUrl = "http://uac.10010.com/portal/Service/CtaIdyChk?verifyCode=" + capthca + "&verifyType=1"; HttpGet verifyCapthcaGet = new HttpGet(verifyCaptchaUrl); verifyResponse = httpClient.execute(verifyCapthcaGet); AbstractHttpClient abstractHttpClient = (AbstractHttpClient) httpClient; for (Cookie cookie : abstractHttpClient.getCookieStore().getCookies()) { System.out.println(cookie.getName() + ":" + cookie.getValue()); if (cookie.getName().equals("uacverifykey")) { uvc = cookie.getValue(); } } } while (!EntityUtils.toString(verifyResponse.getEntity()).contains("true")); //登录 String loginurl = "https://uac.10010.com/portal/Service/MallLogin?userName=" + name + "&password=" + pwd + "&pwdType=01&productType=01&verifyCode=" + capthca + "&redirectType=03&uvc=" + uvc; HttpGet loginGet = new HttpGet(loginurl); CloseableHttpResponse loginResponse = httpclient.execute(loginGet); System.out.print("result:" + EntityUtils.toString(loginResponse.getEntity())); //抓取基本信息数据 //jsonp1404663560635({resultCode:"7072",redirectURL:"http://www.10010.com",errDesc:"null",msg:'系统忙,请稍后再试。',needvode:"1"}); HttpPost basicHttpGet = new HttpPost("http://iservice.10010.com/ehallService/static/acctBalance/execute/YH102010005/QUERY_AcctBalance.processData/Result"); saveToLocal(httpclient.execute(basicHttpGet).getEntity(), "chinaunicom.basic.html"); } /** * 写文件到本地 * * @param httpEntity * @param filename */ public static void saveToLocal(HttpEntity httpEntity, String filename) { try { File dir = new File("/JEE/sz-588/workspace/maven-httpclient-demo"); if (!dir.isDirectory()) { dir.mkdir(); } File file = new File(dir.getAbsolutePath() + "/" + filename); FileOutputStream fileOutputStream = new FileOutputStream(file); InputStream inputStream = httpEntity.getContent(); if (!file.exists()) { file.createNewFile(); } byte[] bytes = new byte[1024]; int length = 0; while ((length = inputStream.read(bytes)) > 0) { fileOutputStream.write(bytes, 0, length); } inputStream.close(); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }
生成文件
json格式输出
猜你喜欢
- java抓取html网页数据内容demo
- java通过java.net.HttpURLConnection类抓取网页源码工具类分享
- java使用jsoup实现网页抓取---案例智联招聘求职信息抓取
- java抓取网页中的电子邮件demo
- 百度云java抓取(内含jar包可直接运行)
- java抓取特定网站图片并下载
- java开源网页分析工具htmlunit爬虫抓取搜狐图片到本地的实例
- java通过jsoup框架抓取网易云音乐歌手信息数据
- jsp自定义网页地址实现抓取图片批量下载器
- httpClient与jsoup的结合使用实现网页抓取数据的简单测试
- java爬虫实现之httpClient4.2.1 连接池管理客户端请求 抓取页面简单示例
- python爬虫抓取哪儿网页上的一些字段
请下载代码后再发表评论
文件名:maven-httpclient-demo.zip,文件大小:58.906K
下载
- /
- /maven-httpclient-demo
- /maven-httpclient-demo/.classpath
- /maven-httpclient-demo/.project
- /maven-httpclient-demo/.settings
- /maven-httpclient-demo/.settings/.jsdtscope
- /maven-httpclient-demo/.settings/org.eclipse.jdt.core.prefs
- /maven-httpclient-demo/.settings/org.eclipse.m2e.core.prefs
- /maven-httpclient-demo/.settings/org.eclipse.wst.common.component
- /maven-httpclient-demo/.settings/org.eclipse.wst.common.project.facet.core.xml
- /maven-httpclient-demo/.settings/org.eclipse.wst.jsdt.ui.superType.container
- /maven-httpclient-demo/src
- /maven-httpclient-demo/src/main
- /maven-httpclient-demo/src/main/java
- /maven-httpclient-demo/src/main/java/com
- /maven-httpclient-demo/src/main/java/com/httpclient
- /maven-httpclient-demo/src/main/java/com/httpclient/asm
- /maven-httpclient-demo/src/main/java/com/httpclient/asm/demo
- /maven-httpclient-demo/src/main/java/com/httpclient/asm
- /maven-httpclient-demo/src/main/java/com/httpclient
- /maven-httpclient-demo/src/main/java/com
- /maven-httpclient-demo/src/main/java
- /maven-httpclient-demo/src/main
- /maven-httpclient-demo
相关代码
最近下载
mq13947193109 LV19
2022年7月5日
xxx159 LV1
2021年4月17日
Matrix12312312 LV1
2019年11月13日
z_yong76 LV26
2019年1月10日
chinafjfzlj LV31
2018年12月26日
elan19 LV5
2018年9月21日
潇love梦 LV9
2018年5月21日
deng214 LV9
2018年3月22日
Madman LV10
2017年10月24日
wsz642531 LV4
2017年9月15日
最近浏览
2036495585 LV9
2023年9月25日
heqian LV16
2023年1月10日
微信网友_5992582549164032 LV6
2022年12月12日
微信网友_6040315240812544 LV8
2022年11月3日
dayuln LV8
2022年4月27日
有时候简简单单就好了 LV4
2022年4月16日
15227959767
2022年4月14日
暂无贡献等级
cx123123 LV7
2022年2月28日
jackylook
2021年12月10日
暂无贡献等级
lw20020421 LV10
2021年6月22日