<= 8; i++) {//这里自行定重试次数client = CheckCodeUtil.getConnection(); // 获取连接qparams.clear();// param.put(codeName, code);mylog.info(sysName + "第" + i + "次登录");param.put(codeName, getCode(codeUrl).getHtml());// 设置验证码// 添加参数for (Map.Entry enetiy : param.entrySet()) {mylog.debug("添加变量" + enetiy.getKey() + "<---->" + enetiy.getValue());qparams.add(new BasicNameValuePair(enetiy.getKey(), enetiy.getValue()));}//post=setCookie(post);//设置cookiepost.setEntity(new UrlEncodedFormEntity(qparams));// 设置实体response = client.execute(post);String loginRsp = EntityUtils.toString(response.getEntity());if (loginCheck(response, loginRsp)) {Header locationHeader = response.getFirstHeader("Location");if (locationHeader != null) {String location = locationHeader.getValue();System.out.println("重定向地址为:"+location);}mylog.info(sysName + "登录成功");System.out.println(sysName +"登录成功");return new ResponseEntity(loginRsp, 1);}else{System.out.println("登录失败");}}return new ResponseEntity("-1", -1);} catch (UnsupportedEncodingException e) {mylog.error("无法解析编码格式" + e.getMessage());e.printStackTrace();} catch (ClientProtocolException e) {mylog.error("连接池错误" + e.getMessage());e.printStackTrace();} catch (IOException e) {mylog.info("读写错误" + e.getMessage());e.printStackTrace();} catch (Exception e) {mylog.info("读写错误" + e.getMessage());e.printStackTrace();} finally {try {client.close();} catch (IOException e) {e.printStackTrace();} // 关闭连接}return new ResponseEntity();}/*** 获取验证码* * @param codeUrl* @return*/public ResponseEntity getCode(String codeUrl) {CloseableHttpClient client = CheckCodeUtil.getConnection(); // 获取连接HttpGet get = new HttpGet(codeUrl); // 获取地址try {mylog.info(sysName + "开始获取验证码");HttpResponse resp = client.execute(get); // 访问HttpEntity entity = resp.getEntity(); // 获取相应实体String filename = UUID.randomUUID().toString();// 下载分析验证码String path = "";if (System.getProperty("os.name").indexOf("Windows") != -1) {path = "E:\\home\\img\\";} else {path = "/home/file/yjbs/yhjjzrb/";}String imgUrl = path + filename + ".jpg";if (CheckCodeUtil.download(entity.getContent(), imgUrl)) {String code = "";System.out.println("下载到验证码图片:"+imgUrl);//code = CheckCodeUtil.getComplexCode(imgUrl,codeUrl);code = CheckCodeUtil.analysisCode(imgUrl, "3", codeUrl);mylog.info("可能正确的验证码:" + code.trim());return new ResponseEntity(code.trim(), resp.getStatusLine().getStatusCode());}else{System.out.println("未能下载到"+sysName+"验证码");};} catch (ConnectException e) {e.printStackTrace();mylog.error(sysName + "连接超时" + e.getMessage());} catch (IOException e) {e.printStackTrace();mylog.error(sysName + "IOException 读写错误" + e.getMessage());} finally {get.abort();try {client.close();} catch (IOException e) {mylog.error("IOException 关闭错误" + e.getMessage());e.printStackTrace();}}return null;}/*** 测试服务器是否开启* * @param url* @return* @throws IOException* @throws ClientProtocolException*/public boolean isOpenService(String url) throws ClientProtocolException, IOException {CloseableHttpClient client = CheckCodeUtil.getConnection();HttpPost post = new HttpPost(url);HttpResponse response = client.execute(post);String loginRsp = EntityUtils.toString(response.getEntity());post.abort();client.close();// 访问到Nginx界面,或者连接超时if (loginRsp == null || (loginRsp.contains("nginx"))|| (response.getStatusLine().getStatusCode() + "").startsWith("4")) {return false;}return true;}/*** 校验登录是否成功* @param entity* @param rspon* @return*/public boolean loginCheck(HttpResponse entity, String rspon) {// System.out.println(rspon);if (entity.getStatusLine().getStatusCode() == 302) {return true;}return false;}}
验证码校验类:.java;这里你们调用百度的即可;
package com.baidu.api;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URISyntaxException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.http.HttpHost;import org.apache.http.client.CookieStore;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.BasicCookieStore;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.client.HttpClients;import org.apache.log4j.Logger;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;public class CheckCodeUtil {public static final ThreadLocal
- 身份鉴别——Linux配置登录失败处理功能
- MySQL启/停 登录失败处理功能
- Windows Server入门系列31 利用LC5破解用户密码
- 最新 详解如何模拟登录知乎 英文验证码以及中文倒立字(继上篇scrapy分析知乎
- 【直播系列之二】快速接入PC端直播功能
- 电脑小技巧系列——私密文件的保护措施
- 二 2021SC@SDUSC PALISADE开源库CKKS讲解系列(二)完整
- CSS3系列 01 基础知识
- 电子器件系列50:开关二极管BAV99
- Python3趣味系列题9-------一笔画完