HttpClient4登陆有验证码的网站

网友投稿 768 2022-11-26

HttpClient4登陆有验证码的网站

HttpClient4登陆有验证码的网站

其实就这个问题,本来是很简单的,我自己花了近两个下午才搞定,现在记录一下。也希望能帮助后来的朋友。

先说大致的处理流程

以下面的login.jsp为例

然后关于验证码的读取,我现在使用的是人工读取,下一个步骤就是引入第三方的接口。

我们看代码说话

package com.zw;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java-.URLEncoder;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import org.apache.commons.io.IOUtils;import org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.* 这个用来测试 bjadmin */public class HttpProxyZW { //下面这两个url 第一个是登陆用的地址 第二个是获得验证码的地址 private static String urlValidate=" private static String urlGetCookieAndValidate=" private String cookie = null; /** * 以行为单位读取文件,常用于读面向行的格式化文件 */ public static String getYZM(String fileName) { InputStream in = null; try { Thread.sleep(10000); in = new FileInputStream(new File(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } Scanner s = new Scanner(in); while (s.hasNextLine()) { return s.nextLine(); } return null; } public static void main(String[] args) { HttpProxyZW hp = new HttpProxyZW(); hp.getPicAndCookie(hp.urlGetCookieAndValidate); List params = hp.getParameter(); hp.getResponse2(hp.urlValidate, params); } public List getParameter(){ List params=new ArrayList(); params.add(new BasicNameValuePair("eticketNoORIn","123456")); params.add(new BasicNameValuePair("passengerName_src","啦啦啦")); try { params.add(new BasicNameValuePair("passengerName",URLEncoder.encode("啦啦啦", "utf-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } params.add(new BasicNameValuePair("validateFlag","0")); params.add(new BasicNameValuePair("invoiceNo","")); params.add(new BasicNameValuePair("imgSrc","/tsky/images/loading.gif")); params.add(new BasicNameValuePair("eticketNo","123456")); params.add(new BasicNameValuePair("invoiceNo","")); //这个目前比较操蛋 //在e盘下 我-了验证码 //然后人工读出 后把字符写进src/yzm.txt String randCode=getYZM("src/yzm.txt"); System.out.println(randCode); params.add(new BasicNameValuePair("code", randCode)); return params; } private void downloadJPG(HttpResponse throws IOException { InputStream input = OutputStream output = new FileOutputStream(new File("E:\\1.jpg")); IOUtils.copy(input, output); if (output != null) { output.close(); } output.flush(); } /* * 获取第一次-图片的时候 远程服务器给我发的cookie */ public String getCookie(HttpResponse throws ClientProtocolException, IOException{ String cookie=""; Header[] headers= //BIGipServerpool_ip_port 这个参数是和负载均衡相关的 //JSESSIONID 这就是sessionid for(Header h:headers){ if (h.getValue().contains("BIGipServerpool_122")) { String BIGipServerpool=h.getValue().substring("BIGipServerpool_122.119.122.179_80=".length(), h.getValue().indexOf(";")); cookie+="BIGipServerpool_122.119.122.179_80="+BIGipServerpool; } if (h.getValue().contains("JSESSIONID")) { String JSESSIONID=h.getValue().substring("JSESSIONID=".length(), h.getValue().indexOf(";")); cookie+="JSESSIONID="+JSESSIONID; } //这些参数的作用 我不清楚 加上就是了 cookie+="Hm_lvt_486e71cc1c3c5d7a07853a6e72364f55=1456716098;__utma=88932958.523101033.1456716098.1456716098.1456716098.1;__utmz=88932958.1456716098.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);CNZZDATA1256052643=1173832624-1456716029-%7C1456884431"; } return cookie; } public CloseableHttpResponse getPicAndCookie(String url) { CloseableHttpClient = HttpClientBuilder.create().build(); HttpPost = new HttpPost(url); CloseableHttpResponse response =null; try { response = getCookie(response); downloadJPG(response); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public void getResponse2(String url,List params) { CloseableHttpClient = HttpClientBuilder.create().build(); HttpPost = new HttpPost(url); "travelsky.com"); "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"); "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); " cookie); "keep-alive"); try { if (params != null) UrlEncodedFormEntity(params, "utf-8")); CloseableHttpResponse response = HttpEntity entity = response.getEntity(); String jsonStr = EntityUtils.toString(entity, "utf-8"); System.out.println("final result" + jsonStr); } catch (Exception e) { e.printStackTrace(); } } }

参考资料

​​ Cookie/Session机制详解​​

​​HttpClient4.x 使用cookie保持会话​​

​​HttpClient 解决中文乱码​​

​​使用HttpClient时遇到的 java-.SocketException: Socket closed异常​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:centos7安装jdk,tomcat,msyql(MariaDB)
下一篇:大型网站技术架构 读书笔记4 高可用架构
相关文章

 发表评论

暂时没有评论,来抢沙发吧~