学信网的登录有验证码,是那种计算数字或者汉字识别的,很难识别。最近连学籍信息和学历信息也换成图片了,常规的正则 css xpath都不能使。
下载图片,需要先登录,获取登陆后的cookie和学籍信息的图片url。这一步由webview获得,用户自行登录,不需要理会验证码。
使用okhttp。需要导入这两个包。
package com.touna.httprequest; import java.io.File; import java.io.FileOutputStream; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class HttpReq { //private OkHttpClient client = new OkHttpClient(); private final Request.Builder builder = new Request.Builder(); private final OkHttpClient client = new OkHttpClient.Builder() .readTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .build(); //设置各种超时时间 /** @param isAddXuexinHeader 是否加入学信网header到请求头 */ public HttpReq(boolean isAddXuexinHeader){ if (isAddXuexinHeader == true){ builder.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); builder.addHeader("Accept-Encoding","gzip, deflate, sdch, br"); builder.addHeader("Accept-Language","zh-CN,zh;q=0.8"); builder.addHeader("Cache-Control","max-age=0"); builder.addHeader("Connection","keep-alive"); builder.addHeader("Host","my.chsi.com.cn"); builder.addHeader("If-Modified-Since","Tue, 13 Mar 2018 10:14:31 GMT"); builder.addHeader("Upgrade-Insecure-Requests","1"); builder.addHeader( "User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"); } } /** @param cookie 加入cookie到请求头 */ public void addCookieToHeader(String cookie){ builder.addHeader("cookie",cookie); } /** @param url get请求的url */ public String get(String url) { builder.url(url); final Request request = builder.build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); }catch (Exception e){ e.printStackTrace(); return ""; } } /** @param url 图片的地址 * */ public void savePicture(String url) { builder.url(url); final Request request = builder.build(); try (Response response = client.newCall(request).execute()) { byte[] bt = response.body().bytes(); byte2image(bt, "./xueji.png"); }catch (Exception e){ e.printStackTrace(); } } private void byte2image(byte[] data,String path){ try { File file = new File(path); FileOutputStream fos = new FileOutputStream(file); fos.write(data, 0, data.length); fos.flush(); fos.close(); System.out.println("保存图片成功。。"); } catch (Exception e) { e.printStackTrace(); } } public static void testBaidu(){ HttpReq baiduHttp = new HttpReq(false); String respStr = baiduHttp.get("http://www.baidu.com"); System.out.println(respStr); } public static void main(String[] args) { // testBaidu(); HttpReq xuexinHttp = new HttpReq(true); xuexinHttp.addCookieToHeader("JSESSIONID=06E39F1AE8D1EA9BFD8F34D"); //由webview执行js返回。 xuexinHttp.savePicture("https://my.chsi.com.cn/archive/gdjy/photo/show.action?pid=2367c5f88b78b088e484a51eb3"); //由webview执行js返回。
}
}