Java 如何抓到该 SSL 图片?

2015-07-11 20:04:50 +08:00
 halo
https://t.williamgates.net/image-3CFE_55A097D8.jpg

试过了,各种不行。。。
希望能给出代码,谢谢!
2651 次点击
所在节点    问与答
11 条回复
mgcnrx11
2015-07-11 20:17:24 +08:00
最基本的SSL方式就能下载到吧?
andybest
2015-07-11 20:34:34 +08:00
@mgcnrx11 我试了似乎不行,给下代码看看?
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
2015-07-11 21:41:25 +08:00
@mgcnrx11 请问是否测试过?这是 https 协议下的图片,你贴的这段代码只是基本的 http request(而且返回值还是String)
nikoo
2015-07-11 21:46:09 +08:00
为什么没有回复差评,对这种连题目都不看就乱贴大段无效代码的行为深恶痛绝
wdlth
2015-07-11 23:19:29 +08:00
看了一下你发的那个URL,用的是CloudFlare SNI SSL,并且使用ECC证书,你要解决这两个问题。Java 8有相应的SNI函数,Java 7可以用 HttpClient。ECC支持度不是很了解,在StackOverflow搜了一下也没找到几个有用的解答。
andybest
2015-07-11 23:30:10 +08:00
@mgcnrx11 我试了你的代码不行,看 @wdlth 的回复可能是版本号的原因,请问你是用的 jdk 版本号是多少?
reeco
2015-07-11 23:33:54 +08:00
我在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
2015-07-12 10:33:37 +08:00
@nikoo
@halo
URLConnection.openConnection()返回的实例根据http还是https协议决定的,HttpURLConnection的子类是HttpsURLConnection。所以代码根本没问题。某楼说得对,我用的是JDK8,估计是JDK的证书验证问题,可以创建一个自定义TM类解决。

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

自己好好学学,看看文档
mgcnrx11
2015-07-12 10:38:04 +08:00
顺便再吐槽,楼主你就一个说不行,不贴出错误信息。牛人都懒得理你,鬼知道你错在哪。
ob
2015-07-12 11:03:27 +08:00
@mgcnrx11 呼呼└(^O^)┘

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/205014

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX