给一个可以下载的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(); (5000); (5000); ("GET"); if (() == HttpURLConnection.HTTP_OK) { InputStream inputStream = (); byte[] bytes= (inputStream); (); return bytes; } } catch (IOException e) { ("获取网络图片出现异常,图片路径为:" ,e); }finally { if(connection!=null){ (); } } 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(); (5000); (5000); ("GET"); responseCode = () ; if (responseCode == HttpURLConnection.HTTP_OK) { String fileName = ("Content-Disposition"); if(fileName != null){ fileName = new String(("ISO-8859-1"),"GBK").split("=")[1]; } InputStream inputStream = (); byte[] bytes= (inputStream); if((fileName)){ fileName=(("/")+1); } ("fileName",fileName); ("bytes",bytes); (); return resultMap; } } catch (IOException e) { ("获取网络文件出现异常,网络路径为:" ,e); }finally { ("请求连接的返回的响应码ResponseCode:"+responseCode); if(connection!=null){ (); } } return null; } /** * 获取网络图片流 * * @param url * @return */ public static byte[] getNetWorkStreamHttps(String url) throws IOException, NoSuchProviderException, NoSuchAlgorithmException { SSLContext sslcontext = ("SSL","SunJSSE"); try { (null, new TrustManager[]{new MyX509TrustManager()}, new ()); } catch (KeyManagementException e) { (); } HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslsession) { ("WARNING: Hostname is not matched for cert."); return true; } }; URL myURL = new URL(url); // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象 HttpsURLConnection connection = (HttpsURLConnection) (); (()); (ignoreHostnameVerifier); try { (5000); (5000); ("GET"); if (() == HttpURLConnection.HTTP_OK) { InputStream inputStream = (); byte[] bytes= (inputStream); (); return bytes; } } catch (IOException e) { ("获取网络图片出现异常,图片路径为:" ,e); }finally { if(connection!=null){ (); } } return null; } /** * 获取网络图片流头部的文件名后缀 * resultMap: key[fileName、bytes] * @param url * @return */ public static Map<String,Object> getNetWorkStreamAndUrlFileName(String url) throws IOException, NoSuchProviderException, NoSuchAlgorithmException { SSLContext sslcontext = ("SSL","SunJSSE"); try { (null, new TrustManager[]{new MyX509TrustManager()}, new ()); } catch (KeyManagementException e) { (); } HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslsession) { ("WARNING: Hostname is not matched for cert."); return true; } }; URL myURL = new URL(url); // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象 HttpsURLConnection connection = (HttpsURLConnection) (); (()); (ignoreHostnameVerifier); //之后任何Https协议网站皆能正常访问,同第一种情况 //HttpURLConnection connection=null; int responseCode = -100 ; Map resultMap = new HashMap(); try { //connection = (HttpURLConnection) new URL(url).openConnection(); (5000); (5000); ("GET"); responseCode = () ; if (responseCode == HttpURLConnection.HTTP_OK) { String fileName = ("Content-Disposition"); if(fileName != null){ fileName = new String(("ISO-8859-1"),"GBK").split("=")[1]; } InputStream inputStream = (); byte[] bytes= (inputStream); if((fileName)){ fileName=(("/")+1); } ("fileName",fileName); ("bytes",bytes); (); return resultMap; } } catch (IOException e) { ("获取网络文件出现异常,网络路径为:" ,e); }finally { ("请求连接的返回的响应码ResponseCode:"+responseCode); if(connection!=null){ (); } } return null; } public static void main(String[] args) throws NoSuchProviderException, NoSuchAlgorithmException, IOException { String url = ""; byte[] a = (url); (a); }
public class MyX509TrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate certificates[],String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] ax509certificate,String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { // TODO Auto-generated method stub return null; } }
还是连接超时,最后找到问题根源是因为信息安全做了域名限制策略,只能用域名访问而不能用ip,但是银行方用的是ip访问所以连接不上
这种问题要找到运维和信息安全处理,了解行方访问方式是用ip的,
通过断开网络和连接网络来反复测试,telnet ip 端口不行,telnet 域名 端口可以,
ok 解决了~