欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

识别验证码并登录案例  

程序员文章站 2024-03-15 13:27:47
...
该案例目的 利用代码模拟http请求达到登录成功实例;

1.通过获取网页验证码URL 识别验证码,目前使用到的技术为;tess4j

Maven

	<dependency>
		<groupId>net.java.dev.jna</groupId>
		<artifactId>jna</artifactId>
		<version>4.1.0</version>
	</dependency>
	
	<dependency>
		<groupId>net.sourceforge.tess4j</groupId>
			<artifactId>tess4j</artifactId>
			<version>2.0.1</version>
			<exclusions>
			<exclusion>
			<groupId>com.sun.jna</groupId>
			<artifactId>jna</artifactId>
			</exclusion>
		</exclusions>
	</dependency>



PS:尽量把这个库写在maven 最前面之前自己测试的时候老是报

java.lang.NoSuchFieldError: SIZE_T_SIZE

辗转分析良久 把这个库设置到前面就解决问题

首先获取解析验证码

这里要注意一下的是 session 和验证码绑定在一起的所以要想验证码通过得获取cookie

/**
	 * 解析图片验证码 并返回 cookie 和验证码
	 * */
	public  String[] orcImage(String url){
		String[] ary = new String[2];
		InputStream inputStream=null;
		try {  
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			HttpURLConnection  connection =  (HttpURLConnection) realUrl.openConnection();
			if (connection.getResponseCode()!=200){
				return null;
			}
			inputStream = connection.getInputStream();
			String str = connection.getHeaderField("Set-Cookie").split(";")[0];
			ary[0] = str;
//			downloadJPG(inputStream);
			BufferedImage bi = ImageIO.read(inputStream);  
			ITesseract instance = new Tesseract();  // JNA Interface Mapping  
			File tessDataFolder = LoadLibs.extractTessResources("tessdata");
			instance.setDatapath(tessDataFolder.getAbsolutePath());
			instance.setLanguage("eng");//英文库识别数字比较准确
            String result = instance.doOCR(bi); 
            ary[1] = result.trim();
            return ary;
         } catch (Exception e) { 
        	 log.error("解析验证码失败!{}",e);
        	 return null;
         } finally {
             try {
				inputStream.close();
			} catch (IOException e) {
			}
		}
	}


解析成功后在调用登录请求
/**
	 * 登录动作
	 * */
	public String login(String url,String codeUrl,String param){
		String[] rel = orcImage(codeUrl);
		param = param+"&inVerifyID="+rel[1];
		BufferedReader in = null;
		String cookie = null;
		try {
			String urlNameString = url + "?" + param;
			log.info("GET 请求:{}",urlNameString);
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection  connection =  realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("Accept", "*/*");
			connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
			connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Host", "www.scsqzx.com");
			connection.setRequestProperty("Cookie",rel[0]);
			connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0");
			// 建立实际的连接
			connection.connect();
			String encoding = connection.getContentEncoding();
//			  //因为返回的是gzip 的数据所以要解析出来
			String xmlStr = readStream(connection,encoding);
                       //因为解析出来的数据是XML 格式的所以解析xml
			String code = xml.getText("Result", xmlStr);
			if(code != null && code.equals("1054")){
				cookie = rel[0];
			}else{
				log.error("登录返回状态{},cookie:{}",xmlStr,rel[0]);
			}
		} catch (Exception e) {
			log.error("登录异常:",e);
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
			}
		}
		
		return cookie;
		
	}


该实例流程完毕