1 public class MyTest { 2 3 public static void main(String[] args) throws Exception { 4 // TODO Auto-generated method stub 5 MyTest ins = new MyTest(); 6 ins.selectTest(); 7 } 8 9 public void selectTest() throws Exception { 10 // 第一步:下载验证码到本地 11 String url = "http://wap.js.10086.cn/imageVerifyCode.jsp"; 12 String destfilename = "F:\\yz.png"; 13 HttpClient httpclient = new DefaultHttpClient(); 14 HttpGet httpget = new HttpGet(url); 15 File file = new File(destfilename); 16 if (file.exists()) { 17 file.delete(); 18 } 19 HttpResponse response = httpclient.execute(httpget); 20 HttpEntity entity = response.getEntity(); 21 InputStream in = entity.getContent(); 22 try { 23 FileOutputStream fout = new FileOutputStream(file); 24 int l = -1; 25 byte[] tmp = new byte[2048]; 26 while ((l = in.read(tmp)) != -1) { 27 fout.write(tmp); 28 } 29 fout.close(); 30 } finally { 31 in.close(); 32 } 33 httpget.releaseConnection(); 34 35 // 第二步:用Post方法带若干参数尝试登录,手动输入下载验证码中显示的数字 36 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 37 System.out.println("请输入验证码——"); 38 String yan = br.readLine(); 39 HttpPost httppost = new HttpPost( 40 "http://wap.js.10086.cn/actionDispatcher.do"); 41 List<NameValuePair> params = new ArrayList<NameValuePair>(); 42 params.add(new BasicNameValuePair("pageNum", "login")); 43 params.add(new BasicNameValuePair("busiNum", "login")); 44 params.add(new BasicNameValuePair("ver", "s")); 45 params.add(new BasicNameValuePair("operType", "0")); 46 params.add(new BasicNameValuePair("loginType", "0")); 47 params.add(new BasicNameValuePair("mobile", "15851865809")); 48 params.add(new BasicNameValuePair("password", "111111")); 49 params.add(new BasicNameValuePair("verifyCode", yan)); 50 params.add(new BasicNameValuePair("isSavePasswordVal", "1")); 51 httppost.setEntity(new UrlEncodedFormEntity(params)); 52 response = httpclient.execute(httppost); 53 entity = response.getEntity(); 54 // 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断登录是否成功 55 String postResult = EntityUtils.toString(entity, "GBK"); 56 // 这里只是简单的打印出当前Cookie值以判断登录是否成功。 57 List<Cookie> cookies = ((AbstractHttpClient) httpclient) 58 .getCookieStore().getCookies(); 59 // for(Cookie cookie: cookies) 60 // System.out.println(cookie); 61 httppost.releaseConnection(); 62 63 // 第三步:最后得到的postResult1中包含所要查询号码的集团、长号、短号 64 HttpPost httppost1 = new HttpPost( 65 "http://wap.js.10086.cn/actionDispatcher.do"); 66 List<NameValuePair> params1 = new ArrayList<NameValuePair>(); 67 params1.add(new BasicNameValuePair("busiNum", "CDHCX")); 68 params1.add(new BasicNameValuePair("operType", "3")); 69 params1.add(new BasicNameValuePair("pageNum", "CDHCX")); 70 params1.add(new BasicNameValuePair("loginInit", "1")); 71 BufferedReader br1 = new BufferedReader( 72 new InputStreamReader(System.in)); 73 System.out.println("请输入要查询的号码——"); 74 String number = br1.readLine(); 75 params1.add(new BasicNameValuePair("queryNum", number));// 所要查询的号码 76 params1.add(new BasicNameValuePair("jk", "查询")); 77 httppost1.setEntity(new UrlEncodedFormEntity(params1)); 78 79 HttpResponse response1 = httpclient.execute(httppost1); 80 HttpEntity entity1 = response1.getEntity(); 81 String postResult1 = EntityUtils.toString(entity1, "GBK"); 82 httppost1.releaseConnection(); 83 //System.out.println(postResult1); 84 85 if (!postResult1.contains("集团名称")) 86 System.out.println("查询号码为非集团用户"); 87 else { 88 String regex1 = "集团名称:(.*?)<"; 89 String regex2 = "集团短号:(.*?)<"; 90 String regex3 = "长号:(.*?)<"; 91 Pattern pattern1 = Pattern.compile(regex1); 92 Pattern pattern2 = Pattern.compile(regex2); 93 Pattern pattern3 = Pattern.compile(regex3); 94 Matcher matcher1 = pattern1.matcher(postResult1); 95 Matcher matcher2 = pattern2.matcher(postResult1); 96 Matcher matcher3 = pattern3.matcher(postResult1); 97 if (matcher1.find()) { 98 System.out.println("集团名称:" + matcher1.group(1)); 99 } 100 if (matcher3.find()) { 101 System.out.println("长号: " + matcher3.group(1)); 102 } 103 if (matcher2.find()) { 104 System.out.println("短号:" + matcher2.group(1)); 105 } 106 } 107 108 } 109 }
可以继续完善:
1.登录方式可以继续完善;
2.验证码可以写成自动识别;
3.控制台操作->UI