JAVA获取百度游戏注册验证码并保存到本地

来源:互联网 发布:c语言入门自学书籍推荐 编辑:程序博客网 时间:2024/05/17 07:18
package downloadimg;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;/** * @author Cytosine * @thanks BreakShadow * get CAPTCHA Img URL's API:https://passport.baidu.com/v2/?reggetcodestr * The CAPTCHA Img'url is "https://passport.baidu.com/cgi-bin/genimage?"add verifystr that get by above API. */public class DownloadImgWithoutType {    public static void main(String args[]){        for(int i=0;i<1000;i++){            String path="D:\\test\\";       //Please replace your path where you want to save it of my basicPath.            BufferedImage bi=httpsGetImg(httpsGetImgURL());            if(bi==null){                System.out.println("bi==null");            }else{                File f=new File(path+i+".png");                try {                    ImageIO.write(bi, "png", f);                } catch (IOException ex) {                    Logger.getLogger(DownloadImgWithoutType.class.getName()).log(Level.SEVERE, null, ex);                }            }        }    }    public static String httpsGetImgURL(){        StringBuffer requestResult=new StringBuffer();        BufferedReader bufferedReader=null;        try{            SSLContext ssl=SSLContext.getInstance("TLS");            ssl.init(null, new TrustManager[]{cytoX509TrustManager}, null);            URL url=new URL("https://passport.baidu.com/v2/?reggetcodestr");            HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();            conn.setSSLSocketFactory(ssl.getSocketFactory());            conn.setRequestMethod("GET");            conn.setDoOutput(true);            conn.connect();            bufferedReader=new BufferedReader(new InputStreamReader(conn.getInputStream()));        }catch(Exception ex){            ex.printStackTrace();        }        try{            String line;            while((line=bufferedReader.readLine())!=null){                requestResult.append(line);            }            bufferedReader.close();        }catch(Exception ex){            ex.printStackTrace();        }        String all=requestResult.toString();        String[] spl=all.split("'");        String verifystr=spl[3];        String result="https://passport.baidu.com/cgi-bin/genimage?"+verifystr;        return result;    }    public static BufferedImage httpsGetImg(String url){        BufferedInputStream bufferedInputStream=null;        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();        try{            SSLContext ssl=SSLContext.getInstance("TLS");            ssl.init(null, new TrustManager[]{cytoX509TrustManager}, null);            URL realURL=new URL(url);            HttpsURLConnection conn=(HttpsURLConnection)realURL.openConnection();            conn.setSSLSocketFactory(ssl.getSocketFactory());            conn.setRequestMethod("GET");            conn.connect();            bufferedInputStream=new BufferedInputStream(conn.getInputStream());        }catch(Exception ex){            ex.printStackTrace();        }        try{            int b;            while((b=bufferedInputStream.read())!=-1){                byteArrayOutputStream.write(b);            }            bufferedInputStream.close();        }catch(Exception ex){            ex.printStackTrace();        }        byte[] imgData=byteArrayOutputStream.toByteArray();        ByteArrayInputStream in=new ByteArrayInputStream(imgData);        BufferedImage result=null;        try {            result = ImageIO.read(in);        } catch (IOException ex) {            Logger.getLogger(DownloadImgWithoutType.class.getName()).log(Level.SEVERE, null, ex);        }        return result;    }    //https证书    private static X509TrustManager cytoX509TrustManager=new X509TrustManager(){        public X509Certificate[] getAcceptedIssuers(){                return null;        }        @Override        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {        }        @Override        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {        }    };}

1 0