V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
halo
V2EX  ›  问与答

Java 如何抓到该 SSL 图片?

  •  1
     
  •   halo · 2015-07-11 20:04:50 +08:00 via Android · 2625 次点击
    这是一个创建于 3183 天前的主题,其中的信息可能已经有所发展或是发生改变。
    https://t.williamgates.net/image-3CFE_55A097D8.jpg

    试过了,各种不行。。。
    希望能给出代码,谢谢!
    11 条回复    2015-07-12 11:03:27 +08:00
    mgcnrx11
        1
    mgcnrx11  
       2015-07-11 20:17:24 +08:00
    最基本的SSL方式就能下载到吧?
    andybest
        2
    andybest  
       2015-07-11 20:34:34 +08:00
    @mgcnrx11 我试了似乎不行,给下代码看看?
    mgcnrx11
        3
    mgcnrx11  
       2015-07-11 21:01:08 +08:00
    private static String httpRequest(String url, String requestMethod, String postParameters) {
    StringBuilder resultString = new StringBuilder();

    HttpURLConnection connection = null;
    try {
    URL urlGet = new URL(url);
    // 真正连接在调用http.connect()时;
    connection = (HttpURLConnection) urlGet.openConnection();
    // 连接超时
    connection.setConnectTimeout(CONNECT_TIMEOUT);
    // 读取超时 --服务器响应比较慢,增大时间
    connection.setReadTimeout(READ_TIMEOUT);
    // 设置Http请求method
    connection.setRequestMethod(requestMethod);

    connection.setDoOutput(true);
    connection.setDoInput(true);

    if (postParameters != null && requestMethod.equalsIgnoreCase(HTTP_METHOD_POST)) { // Post时调用
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", "" +
    Integer.toString(postParameters.getBytes("UTF-8").length));

    // 隐式调用 connection.connect();
    OutputStream out = connection.getOutputStream();
    out.write(postParameters.getBytes(DEFAULT_CHARSET));
    out.flush();
    out.close();
    }

    // 隐式调用 connection.connect();
    InputStream in = connection.getInputStream();
    BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
    String valueString;

    while ((valueString = read.readLine()) != null) {
    resultString.append(valueString);
    }
    in.close();

    } catch (IOException e) {
    LOGGER.error("Https请求出错!", e);
    } finally {
    if (connection != null) {
    connection.disconnect();
    }
    }
    return resultString.toString();
    }
    halo
        4
    halo  
    OP
       2015-07-11 21:41:25 +08:00 via Android
    @mgcnrx11 请问是否测试过?这是 https 协议下的图片,你贴的这段代码只是基本的 http request(而且返回值还是String)
    nikoo
        5
    nikoo  
       2015-07-11 21:46:09 +08:00
    为什么没有回复差评,对这种连题目都不看就乱贴大段无效代码的行为深恶痛绝
    wdlth
        6
    wdlth  
       2015-07-11 23:19:29 +08:00   ❤️ 1
    看了一下你发的那个URL,用的是CloudFlare SNI SSL,并且使用ECC证书,你要解决这两个问题。Java 8有相应的SNI函数,Java 7可以用 HttpClient。ECC支持度不是很了解,在StackOverflow搜了一下也没找到几个有用的解答。
    andybest
        7
    andybest  
       2015-07-11 23:30:10 +08:00
    @mgcnrx11 我试了你的代码不行,看 @wdlth 的回复可能是版本号的原因,请问你是用的 jdk 版本号是多少?
    reeco
        8
    reeco  
       2015-07-11 23:33:54 +08:00   ❤️ 1
    我在sf上已经回答你了,升级你的jdk吧

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;

    /**
    * Created by reeco_000 on 2015/7/11.
    */
    public class Image {

    public static void main(String[] args) {
    BufferedImage image = null;
    try {
    URL url = new URL("https://t.williamgates.net/image-3CFE_55A097D8.jpg");
    image = ImageIO.read(url);
    } catch (IOException e) {
    e.printStackTrace();
    }

    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(image));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
    mgcnrx11
        9
    mgcnrx11  
       2015-07-12 10:33:37 +08:00
    @nikoo
    @halo
    URLConnection.openConnection()返回的实例根据http还是https协议决定的,HttpURLConnection的子类是HttpsURLConnection。所以代码根本没问题。某楼说得对,我用的是JDK8,估计是JDK的证书验证问题,可以创建一个自定义TM类解决。

    再说,二楼我发帖的时候已经自己验证过了。

    自己好好学学,看看文档
    mgcnrx11
        10
    mgcnrx11  
       2015-07-12 10:38:04 +08:00
    顺便再吐槽,楼主你就一个说不行,不贴出错误信息。牛人都懒得理你,鬼知道你错在哪。
    ob
        11
    ob  
       2015-07-12 11:03:27 +08:00 via Android
    @mgcnrx11 呼呼└(^O^)┘
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3485 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 10:34 · PVG 18:34 · LAX 03:34 · JFK 06:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.