http和https连接下载
给一个可以下载的http地址和https地址,分别用不同的方式来下载,https需要SSL证书, 另外呢,http和https如何转换? 不要用https来访问http 解决了蛮久的,最后发现是网络原因,外网连接不上,所以联调的时候,用内网和外网分别请求,先排除网络原因. (为啥没想到断开网络用外网连接请求呢?感觉思路啊,还是得多训练这种思维和处理问题的方式)/*** 获取网络图片流** @param url* @return*/ public static byte[] getNetWorkStream(String url) {HttpURLConnection connection=null;try {connection = (HttpURLConnection) new URL(url).openConnection();connection.setReadTimeout(5000);connection.setConnectTimeout(5000);connection.setRequestMethod("GET");if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = connection.getInputStream();byte[] bytes= IOUtils.toByteArray(inputStream);inputStream.close();return bytes;}} catch (IOException e) {log.error("获取网络图片出现异常,图片路径为:" ,e);}finally {if(connection!=null){connection.disconnect();}}return null; }/*** 获取网络图片流头部的文件名后缀* resultMap: key[fileName、bytes]* @param url* @return*/ public static Map<String,Object> getNetWorkStreamAndFileName(String url) {HttpURLConnection connection=null;int responseCode = -100 ;Map resultMap = new HashMap();try {connection = (HttpURLConnection) new URL(url).openConnection();connection.setReadTimeout(5000);connection.setConnectTimeout(5000);connection.setRequestMethod("GET");responseCode = connection.getResponseCode() ;if (responseCode == HttpURLConnection.HTTP_OK) {String fileName = connection.getHeaderField("Content-Disposition");if(fileName != null){fileName = new String(fileName.getBytes("ISO-8859-1"),"GBK").split("=")[1];}InputStream inputStream = connection.getInputStream();byte[] bytes= IOUtils.toByteArray(inputStream);if(StringUtils.isEmpty(fileName)){fileName=url.substring(url.lastIndexOf("/")+1);}resultMap.put("fileName",fileName);resultMap.put("bytes",bytes);inputStream.close();return resultMap;}} catch (IOException e) {log.error("获取网络文件出现异常,网络路径为:" ,e);}finally {log.info("请求连接的返回的响应码ResponseCode:"+responseCode);if(connection!=null){connection.disconnect();}}return null; }/*** 获取网络图片流** @param url* @return*/ public static byte[] getNetWorkStreamHttps(String url) throws IOException, NoSuchProviderException, NoSuchAlgorithmException {SSLContext sslcontext = SSLContext.getInstance("SSL","SunJSSE");try {sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());} catch (KeyManagementException e) {e.printStackTrace();}HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {@Overridepublic boolean verify(String s, SSLSession sslsession) {System.out.println("WARNING: Hostname is not matched for cert.");return true;}};URL myURL = new URL(url);// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象HttpsURLConnection connection = (HttpsURLConnection) myURL.openConnection();connection.setSSLSocketFactory(sslcontext.getSocketFactory());connection.setHostnameVerifier(ignoreHostnameVerifier);try {connection.setReadTimeout(5000);connection.setConnectTimeout(5000);connection.setRequestMethod("GET");if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = connection.getInputStream();byte[] bytes= IOUtils.toByteArray(inputStream);inputStream.close();return bytes;}} catch (IOException e) {log.error("获取网络图片出现异常,图片路径为:" ,e);}finally {if(connection!=null){connection.disconnect();}}return null; }/*** 获取网络图片流头部的文件名后缀* resultMap: key[fileName、bytes]* @param url* @return*/ public static Map<String,Object> getNetWorkStreamAndUrlFileName(String url) throws IOException, NoSuchProviderException, NoSuchAlgorithmException {SSLContext sslcontext = SSLContext.getInstance("SSL","SunJSSE");try {sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());} catch (KeyManagementException e) {e.printStackTrace();}HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {@Overridepublic boolean verify(String s, SSLSession sslsession) {System.out.println("WARNING: Hostname is not matched for cert.");return true;}};URL myURL = new URL(url);// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象HttpsURLConnection connection = (HttpsURLConnection) myURL.openConnection();connection.setSSLSocketFactory(sslcontext.getSocketFactory());connection.setHostnameVerifier(ignoreHostnameVerifier);//之后任何Https协议网站皆能正常访问,同第一种情况//HttpURLConnection connection=null;int responseCode = -100 ;Map resultMap = new HashMap();try {//connection = (HttpURLConnection) new URL(url).openConnection();connection.setReadTimeout(5000);connection.setConnectTimeout(5000);connection.setRequestMethod("GET");responseCode = connection.getResponseCode() ;if (responseCode == HttpURLConnection.HTTP_OK) {String fileName = connection.getHeaderField("Content-Disposition");if(fileName != null){fileName = new String(fileName.getBytes("ISO-8859-1"),"GBK").split("=")[1];}InputStream inputStream = connection.getInputStream();byte[] bytes= IOUtils.toByteArray(inputStream);if(StringUtils.isEmpty(fileName)){fileName=url.substring(url.lastIndexOf("/")+1);}resultMap.put("fileName",fileName);resultMap.put("bytes",bytes);inputStream.close();return resultMap;}} catch (IOException e) {log.error("获取网络文件出现异常,网络路径为:" ,e);}finally {log.info("请求连接的返回的响应码ResponseCode:"+responseCode);if(connection!=null){connection.disconnect();}}return null; }public static void main(String[] args) throws NoSuchProviderException, NoSuchAlgorithmException, IOException {String url = "";byte[] a = CommonUtils.getNetWorkStreamHttps(url);System.out.println(a); }
public class MyX509TrustManager implements X509TrustManager {@Overridepublic void checkClientTrusted(X509Certificate certificates[],String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] ax509certificate,String s) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {// TODO Auto-generated method stubreturn null;} }
还是连接超时,最后找到问题根源是因为信息安全做了域名限制策略,只能用域名访问而不能用ip,但是银行方用的是ip访问所以连接不上
这种问题要找到运维和信息安全处理,了解行方访问方式是用ip的,
通过断开网络和连接网络来反复测试,telnet ip 端口不行,telnet 域名 端口可以,
ok 解决了~
http和https连接下载相关推荐
- fiddler抓苹果手机上app包的方法,解决https连接只抓到Tunnel to的问题
今天需要用fiddler抓iphone上安装的app包,ios 12.3.1 电脑端用的360浏览器,手机端用的safari 具体步骤如下 1.电脑连入wifi,记下电脑端的ip地址备用 2.手机与电 ...
- JAVA JDK1.6 https协议下载问题记录
JAVA JDK1.6 https协议下载问题记录及通过代理下载 解决方案 TrustManager [] tm = {new MyX509TrusManager()}; SSLContext ssl ...
- charles iphone8 windows 连接 下载证书不能上网
charles iphone8 windows 连接 下载证书不能上网 安装流程参考:https://www.jianshu.com/p/d3b8d2a51416 遇到的问题 :手机请求 charle ...
- java ssl https 连接详解 生成证书
我们在关于Java EE安全的系列文章中,有一篇也详细介绍了如何在Java EE应用中创建SSL连接和证书.正如前面文章提到的,SSL(Secure Sockets Layer,安全套接层)/TLS( ...
- https连接java_如何从Java应用程序设置Https连接
我使用 java创建桌面应用程序,此应用程序使用API.为了保证与API的通信,我们得到了他们支持使用HTTPS的通知.请指导我如何从 Java客户端设置https连接. API具有此功能,表明它 ...
- CentOS安装SVN服务器并配置HTTPS连接
在CentOS6.3 64位机器上配置SVN服务器,并设置只允许HTTPS连接,可以配置多个repos源,每个源都拥有自己的组和成员,用于权限控制. 安装相关软件 Apache yum install ...
- EdgeRouter X设置外网远程访问和HTTPS连接指定出口网关
EdgeRouter X虽然小巧,但功能强大,为方便远程管理,必须对防火墙进行设置,允许从外部进行访问,由于公网的80.443端口都已被运营商关闭,必须设置端口转发才能从外部访问. 一.设置外网远程访 ...
- https连接加载http资源
当https 连接中包含加载http资源时,浏览器会停止加载,UC浏览器console 会打印如下信息: Mixed Content: The page at 'https://Xie.cn/' wa ...
- tomcat7.0.55配置单向和双向HTTPS连接
HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...
- java访问https链接下载图片
java访问https链接下载图片 一.通过maven引入https工具包 <dependency><groupId>org.apache.httpcomponents< ...
最新文章
- 区分json与jsonp
- Scrapy框架中管道的使用
- Linux服务器上最简单的Nginx反向代理配置
- LeetCode5377. 将二进制表示减到1的步骤数
- 多索引表 (2)基本概念
- iOS经典面试题之“runtime是如何实现weak变量的自动置nil”
- 利用Python进行数据分析(1) 简单介绍
- Linux Shell快速入门
- vim打造成C++的IDE
- MATLAB LFCM雷达调频法测距
- 10.TreeSet、比较器
- 使用gulp构建一个项目
- 核心概念——节点/边/Combo——内置Combo——内置Combo总览
- 数据结构 | 合并两个长度分别为m和n的有序表,最坏情况下需要比较m+n-1次
- 服务器必备工具软件推荐
- 51单片机实现电机控制和LCD显示
- scanf函数和回车、空格 及其返回值
- 信息打点-CDN绕过
- 杭电信工微巴士功能技术设计
- 如何理解充分条件和必要条件
热门文章
- (附源码)ssm小米购物网站 毕业设计 261624
- 我的世界服务器显示红心,我的世界手机版红心怎么恢复 | 手游网游页游攻略大全...
- linux系统用虚拟光驱装win7,虚拟光驱安装win7系统步骤
- Java利用MessageDigest获取字符串或文件MD5详解
- MessageDigest详解
- 关于十字翻转棋的解法研究
- 基于三菱PLC的全自动洗衣机控制系统设计
- U盘病毒肆虐横行 金山毒霸2011全面解决隐患
- 宏碁电脑安装linux,ubuntu安装篇——acer 4750G ubuntu安装详解
- 反编译DLL和.NET文件工具dnSpy