1
mgcnrx11 2015-07-11 20:17:24 +08:00
最基本的SSL方式就能下载到吧?
|
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(); } |
4
halo OP @mgcnrx11 请问是否测试过?这是 https 协议下的图片,你贴的这段代码只是基本的 http request(而且返回值还是String)
|
5
nikoo 2015-07-11 21:46:09 +08:00
为什么没有回复差评,对这种连题目都不看就乱贴大段无效代码的行为深恶痛绝
|
6
wdlth 2015-07-11 23:19:29 +08:00 1
看了一下你发的那个URL,用的是CloudFlare SNI SSL,并且使用ECC证书,你要解决这两个问题。Java 8有相应的SNI函数,Java 7可以用 HttpClient。ECC支持度不是很了解,在StackOverflow搜了一下也没找到几个有用的解答。
|
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); } } |
9
mgcnrx11 2015-07-12 10:33:37 +08:00
|
10
mgcnrx11 2015-07-12 10:38:04 +08:00
顺便再吐槽,楼主你就一个说不行,不贴出错误信息。牛人都懒得理你,鬼知道你错在哪。
|