HttpClient兑现透过验证码查询
HttpClient实现透过验证码查询
通过几天时间还是不能破解掉验证码,但是可以通过人工输入实现,这样并没有达到我想要的结果,但是还是记录下来,供大家参考,
通过几天时间还是不能破解掉验证码,但是可以通过人工输入实现,这样并没有达到我想要的结果,但是还是记录下来,供大家参考,
private static Log log = LogFactory.getLog(HttpClientMail.class); public static boolean query(String code,String key) { boolean result = false; String indexurl = "http://app.szaic.gov.cn/CheckPage.WebUI/Index.aspx?PURL=http%3a%2f%2fapp02.szaic.gov.cn%2fWERMIS2.webui%2fWebPages%2fAIC%2fTradeSearch.aspx"; String sendurl = "http://app02.szaic.gov.cn/WERMIS2.webui/WebPages/AIC/TradeSearch.aspx"; String codeurl="http://app02.szaic.gov.cn/WERMIS2.webui/WebPages/WebRegUser/CheckCode.aspx"; try{ // 需登陆后访问的 Url HttpClient httpClient = new HttpClient(); log.debug("HttpClientLogin.getBroadbandNewNo(): 001: postMethod: "); GetMethod getMethod = new GetMethod(indexurl); //设置参数 NameValuePair[] data = { new NameValuePair("PUTL", "http://app02.szaic.gov.cn/WERMIS2.webui/WebPages/AIC/TradeSearch.aspx"), }; getMethod.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); getMethod.setRequestHeader("Accept-Encoding","gzip, deflate"); getMethod.setRequestHeader("Accept-Language", "zh-cn"); getMethod.setRequestHeader("Connection", "Keep-Alive"); getMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; CIBA)"); getMethod.setQueryString(data); //执行 httpClient.executeMethod(getMethod); String getData = new String(getMethod.getResponseBody(),"UTF-8"); //-----------------------------访问首页,获取cookie---------------------------------------------- PostMethod postMethod = new PostMethod(sendurl); //设置参数 NameValuePair[] data1 = { new NameValuePair("hfIp", "121.15.133.155"), }; postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); postMethod.setRequestHeader("Referer", indexurl); postMethod.setRequestBody(data1); httpClient.executeMethod(postMethod); //获取cookie Cookie[] cookies = httpClient.getState().getCookies(); String cookie = ""; for (Cookie c : cookies) { cookie += c.toString() + ";"; } getData = new String(postMethod.getResponseBody(),"UTF-8"); //-----------------------------获取验证码--------------------------------------------- GetMethod getMethod2 = new GetMethod(codeurl); getMethod2.setRequestHeader("cookie",cookie); //执行 httpClient.executeMethod(getMethod2); code = savegif(getMethod2); //------------------------关键字查询操作------------------------- PostMethod postMethod1 = new PostMethod(sendurl); //设置参数 NameValuePair[] data2 = { new NameValuePair("__EVENTARGUMENT", ""), new NameValuePair("__EVENTTARGET", "txtCheckCode"), new NameValuePair("__LASTFOCUS", ""), new NameValuePair("__VIEWSTATE", "Zb73qiSbM0qiPYhYIhU/u9IaFjmjc94zZ4OMyL7PGuex366gM1OHJNbPkuAZeWaZjHuqe923Vrjzn6mrgkcG3QUlepbFyvH4YoI24Kn18xBl+SqpQ6lY8hlKjkiPSJm5SleMQd3QDZ37d3qhGpCgOBvMmlrFt1y898uMkbco0whDJqxHRhlOiIIIhlQuOFvCZyq9+LzedTx5DQ+GmSUHgKJ0669O0LcFZMj5PofIl1bGbiRg5JiSjcq8hfovEQo843plLBzUKtdU/oEMvGcBABu19Zk20RpUicTEy56rWMhX1aGk0OcgEdFYieNhPYa0y5G0RjRWqvBXziV+SGUa2rio8EEqEcUoqRgQ3a1P/WIQGzMq8XofT/ACcsp+OWC2p2LguOQkhciqRBRns8I7mwWgiIAJf8pxJ26Jq3pozNECtomCg7jTAGfDftTMZv83gVTFchExwF7r1qCbfQc5Cs/mT1GgLtRMn4nvwOCCGUeCHCVCeyH8nLp16nIAXLpm00xdjbx2m8amwXzOu87tBbiqRejNCJES5e/VqSB42MT8bKs/Y5pxdC6mJ7fyW1RBruXo/1Q54JEANZKrA281OpzlrVks3/X3q4OOCaHrlXBt9znkEA9rgEjkXOF+4muGMgdjerzHZAnY1YKlo04fS7KPojZChQekj77yotEONp++1NU33yuxOSYfYOc="), new NameValuePair("ScriptManager1", "upTopBtn|btnOnlyTradeName"), new NameValuePair("btnOnlyTradeName","字号查重"), new NameValuePair("txtCheckCode",code), new NameValuePair("txtTradeName", URLDecoder.decode(key,"UTF-8")), }; postMethod1.setRequestHeader("cookie",cookie); postMethod1.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); postMethod1.setRequestBody(data2); httpClient.getParams().setCookiePolicy(org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY); httpClient.executeMethod(postMethod1); getData = new String(postMethod1.getResponseBody(),"UTF-8"); System.out.println(getData); //解析html if(getData.indexOf(URLDecoder.decode(key,"UTF-8"))<=0){ System.out.println("-------没有找到字号为:" + key +"的记录!"); result = false; }else{ String regex = "(<.+?>|</.+?>| )+"; Pattern p = Pattern.compile(regex); String[] array = p.split(getData); String res=""; for (String s : array) { if ("".equals(s)) { continue; } res += s +","; } String res_Ary[] = res.split(","); for(String r:res_Ary){ if(r.indexOf(URLDecoder.decode(key,"UTF-8")) > 0){ System.out.println("您搜索的结果为:" +r); break; } } result = true; } }catch (Exception e) { result = false; log.error("---------------------->>>key" + e + "-----------------------error"); } return result; } public static String savegif(GetMethod getMethod) throws IOException { /*String projectPath = HttpClientMail.class.getResource("").toString().replace("%20", " ").substring(6); projectPath = projectPath.split("/WEB-INF/classes/")[0]; String excelPath = projectPath + "/file/2008sohu.gif";*/ String excelPath = "c:/code.jpg"; File storeFile = new File(excelPath); FileOutputStream output = new FileOutputStream(storeFile); //得到网络资源的字节数组,并写入文件 output.write(getMethod.getResponseBody()); output.close(); JFrame frame= new JFrame(); frame.setVisible(false); frame.setBounds(100, 100, 100, 100); frame.setLayout(new FlowLayout()); ImageIcon icon = new ImageIcon(excelPath); frame.add(new JLabel(icon)); frame.setVisible(true); /** * 在控制台输入验证码 */ System.out.println("-------->>>>>请输入你看到的验证码:"); Scanner scr = new Scanner(System.in); String ValidCode = scr.nextLine(); return ValidCode; } public static String GetRegex(String str, String rexString){ // 生成 Pattern 对象并且编译一个简单的正则表达式"Kelvin" Pattern p = Pattern.compile(rexString); // 用 Pattern 类的 matcher() 方法生成一个 Matcher 对象 Matcher m = p.matcher(str); String result = ""; if(m.find()){ result = m.group(); } return result; } public static void main(String[] args) { try { System.out.println("------------>>>>>请输入关键字:"); Scanner scr = new Scanner(System.in); String key = scr.nextLine(); query("",URLEncoder.encode(key, "UTF-8")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }