首页>代码>apache HttpClient 4.3.4自动登录并抓取中国联通网页用户基本信息和账单数据>/maven-httpclient-demo/src/main/java/com/httpclient/asm/demo/LoginChinaUnicomWithCaptcha.java
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(); } } }
最近下载更多
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日