• 如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]


    http://bbs.csdn.net/topics/390426978

    public static String readCheckImage(HashMap<String, String> requestMap) {
            String url = requestMap.get("url");
            if (requestMap.get("params") != null) {
                url += "?" + requestMap.get("params");
            }
            InputStream in = null;
            try {
                System.out.println("get>>>" + url);

                URL serverUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
                conn.setRequestMethod("GET");// "POST" ,"GET"
                conn.addRequestProperty("Cookie", requestMap.get("cookie"));
                conn.addRequestProperty("Accept-Charset", "UTF-8;");// GB2312,
                conn.addRequestProperty("User-Agent",
                        "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
                conn.connect();
                // 判断请求是否成功
                if (((HttpURLConnection) conn).getResponseCode() == 200) {
                    // 获取所有响应头字段
                    Map<String, List<String>> map = conn.getHeaderFields();
                    // 遍历所有的响应头字段
                    for (String key : map.keySet()) {
                        System.out.println(key + "--->" + map.get(key));
                    }
                    // 返回的cookie
                    String cookie = null;
                    if (conn.getHeaderFields().get("Set-Cookie") != null) {
                        for (String s : conn.getHeaderFields().get("Set-Cookie")) {
                            cookie += s;
                        }
                    }
                    requestMap.put("cookie", requestMap.get("cookie") + cookie);
                    in = conn.getInputStream();

                    BufferedImage bi = ImageIO.read(in);
                    File f = new File("check_img.jpg");
                    ImageIO.write(bi, "jpg", f);

                    return f.getAbsolutePath();
                } else {
                    System.out.println("下载code图片失败!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }// 使用finally块来关闭输入流
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return null;
        }

  • 相关阅读:
    distcc加速内核编译
    ssh不检查server变化
    bbb u-boot SPI 启动
    Debian NAT共享上网
    Debian Epson L455 打印机
    Learn CMake's Scripting Language in 15 Minutes (ZZ)
    网络启动并安装Debian
    GNU LD 脚本学习笔记
    JLink defective
    获取真实mac地址
  • 原文地址:https://www.cnblogs.com/donaldlee2008/p/5281972.html
Copyright © 2020-2023  润新知