WebSphere应用服务器中https 请求协议的相关注意事项(服务器使用代理上Internet)
最近遇到个需求需要web服务器应用通过https方式请求外部Internet服务器的接口,一开始本地测试时使用以下代码:
String businessCode = "SH30580";
GenerateXml xml = new GenerateXml();
String xmlContent = xml.writeXmlString(businessCode);
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, new TrustManager[] {new X509TrustForMSL()},new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
// 创建URL对象
URL myURL = new URL("https://211.144.221.138/mslws/Services/rsa/RsaWebService.svc/security/validateagencyqualification");
System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.38.194.30", 8080));//因为服务器使用代理上Internet,设置代理的ip和端口号
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
httpsConn.setRequestProperty( "Proxy-Authorization", "Basic " + new sun.misc.BASE64Encoder().encode("用户名:密码".getBytes()) );//设置用户名和密码
httpsConn.setSSLSocketFactory(ssf);
// 以POST方式提交
httpsConn.setRequestMethod("POST");
// 设置连接输出
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
// Post 请求不能使用缓存
httpsConn.setUseCaches(false);
httpsConn.setRequestProperty("Content-Type","application/xml");
// httpsConn.connect();
//获取输出流
OutputStream os = httpsConn.getOutputStream();
// // 设置输出流字符集
os.write(xmlContent.getBytes("UTF-8"));
os.flush();
os.close();
// 取得该连接的输入流,以读取响应内容
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
// 读取服务器的响应内容并显示
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class X509TrustForMSL implements X509TrustManager {
/*
* The default X509TrustManager returned by SunX509. We'll delegate
* decisions to it, and fall back to the logic in this class if the
* default X509TrustManager doesn't trust it.
*/
X509TrustManager sunJSSEX509TrustManager;
X509TrustForMSL() throws Exception {
// create a "default" JSSE X509TrustManager.
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("D:\\jssecacerts"),
"changeit".toCharArray());
TrustManagerFactory tmf =
TrustManagerFactory.getInstance("SunX509");//, "SunJSSE"
tmf.init(ks);
TrustManager tms [] = tmf.getTrustManagers();
/*
* Iterate over the returned trustmanagers, look
* for an instance of X509TrustManager. If found,
* use that as our "default" trust manager.
*/
for (int i = 0; i < tms.length; i++) {
if (tms[i] instanceof X509TrustManager) {
sunJSSEX509TrustManager = (X509TrustManager) tms[i];
return;
}
}
/*
* Find some other way to initialize, or else we have to fail the
* constructor.
*/
throw new Exception("Couldn't initialize");
}
/*
* Delegate to the default trust manager.
*/
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
} catch (CertificateException excep) {
// do any special handling here, or rethrow exception.
}
}
/*
* Delegate to the default trust manager.
*/
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
} catch (CertificateException excep) {
/*
* Possibly pop up a dialog box asking whether to trust the
* cert chain.
*/
}
}
/*
* Merely pass this through.
*/
public X509Certificate[] getAcceptedIssuers() {
return sunJSSEX509TrustManager.getAcceptedIssuers();
}
}
Eclipse测试没问题,但是发布到WebSphere中报错,说不支持SunJSSE,查了半天,原来IBM不支持sun公司的jdk中的jsse.jar包,网上说有自己添加的,也有说把证书加入哪里的,或许因为本人愚笨,搞了大半天,p用没有,最后终于悟得以下方法:
public static void main(String args[]) throws Exception{
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, new TrustManager[] {new TrustAnyTrustManager()},new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
// 创建URL对象
URL myURL = new URL("https://211.144.221.138/mslws/Services/rsa/RsaWebService.svc/security/validateagencyqualification");
String businessCode = "SH30580";
GenerateXml xml = new GenerateXml();
String xmlContent = xml.writeXmlString(businessCode);
// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.38.194.30", 8080));
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
httpsConn.setRequestProperty( "Proxy-Authorization", "Basic " + new sun.misc.BASE64Encoder().encode("dajiang:Abcd1234".getBytes()) );
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new TrustAnyHostnameVerifier());
// 以POST方式提交
httpsConn.setRequestMethod("POST");
// 设置连接输出
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
// Post 请求不能使用缓存
httpsConn.setUseCaches(false);
httpsConn.setRequestProperty("Content-Type","application/xml");
// httpsConn.connect();
//获取输出流
OutputStream os = httpsConn.getOutputStream();
// // 设置输出流字符集
os.write(xmlContent.getBytes("UTF-8"));
os.flush();
os.close();
// 取得该连接的输入流,以读取响应内容
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
// 读取服务器的响应内容并显示
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
}
/*
* 用于连接https使用
*/
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)throws CertificateException {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
按这种方法,终于链接成功,大石头落地~!
WebSphere应用服务器中https 请求协议的相关注意事项(服务器使用代理上Internet)的更多相关文章
- Javaweb中的请求路径的相关总结
重定向和转发相对路径和绝对路径问题 注意:转发和重定向的URLString前有加 / 为绝对路径 反之为相对路径 1.假设通过表单请求指定的Url资源 action="LoginServ ...
- nodejs中https请求失败,无报错
今天群里一位同学在做练习的时候,采用https例子: // curl -k https://localhost:8000/ const https = require('https'); const ...
- node中https请求 | 实现https的请求,获取图片,然后转成base64字节码
get请求 下面实现https的请求,获取图片,然后转成base64字节码 this.checkCodeUrl = 'https://www.test.com/kaptcha.jsp'; var ht ...
- python中Requests模块中https请求在设置为忽略有效性验证,屏蔽告警信息的方式
增加下面的就ok了from requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.ur ...
- WebSphere应用服务器内存泄漏探测与诊断工具选择最佳实践
内存泄漏是比较常见的一种应用程序性能问题,一旦发生,则系统的可用内存和性能持续下降:最终将导致内存不足(OutOfMemory),系统彻底宕掉,不能响应任何请求,其危害相当严重.同时,Java堆(He ...
- 项目通过https访问的tomcat相关配置
开发项目已经完成,那么就是要部署项目到服务器上面.我最近把刚完成的项目部署到服务器上面,内网通过http协议进行访问一切正常,但是测试外网通过https协议进行访问的时候就出现了一些js文档找不到的b ...
- Charles如何抓取https请求-移动端+PC端
Charles安装完成,默认只能抓取到http请求,如果查看https请求,会显示unkonw或其它之类的响应.所以需要先进行一些配置,才能抓取到完整的https请求信息.下面针对PC端和手机端抓包的 ...
- HttpClient中post请求http、https示例
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...
- C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求 (转载)
这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全部采用手工操作工作量大而且繁琐,因此周公决定利用C#来实现.在很多地方都需要验证用户身份才可 ...
随机推荐
- 【转】 CoreGraphics QuartzCore CGContextTranslateCTM 用法
原文:http://blog.csdn.net/sqc3375177/article/details/25708447 CoreGraphics.h 一些常用旋转常量 #define M_E 2.71 ...
- Ubuntu 12.04下解决Rhythmbox Music Player乱码问题
1.打开终端输入如下信息: $ sudo gedit ~/.profile 2.在打开的文档末尾加上如下两句: export GST_ID3_TAG_ENCODING=GBK:UTF-8:GB1803 ...
- Invalid project description overlaps the location of another project [android]
解决办法: 1.将工程放到其他目录下,然后执行Android工程的导入,导入时可以选择“Copy projects into workspace”: 2.不用Android工程导入,而用普通的工程导入 ...
- 【BZOJ2049】【LCT】Cave 洞穴勘测
Description 辉 辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通 道组成,并且每条通道连接了 ...
- 算法系列之图--BFS
广度优先搜索以源结点s为出发点,算法始终将已发现和未发现结点之间的边界,沿其广度方向向外扩展.也即算法需要在发现所有距离源结点s为k的所有结点之后才会去发现距离源结点距离为k+1的其他结点. talk ...
- Excel等外部程序点击链接会带上IE信息的bug
今天碰到一个问题,在Excel内点击链接到默认浏览器Chrome打开,奇怪的是服务端收到的Session一直对不上. 查了很久发现这个Excel到Chrome的跳转竟然带上了IE的Cookie 和 U ...
- H5中需要掌握的 ANIMATION 动画效果
CSS3的动画在PC网页上或者APP上用得越来越多,比如H5页面的应用,目前在营销传播上的意义比较大,还有企业官网或者APP主要介绍也用得比较多,当然还有很多地方都用到.所以学习css的动画也迫在眉睫 ...
- javascript 设为首页 | 加入收藏夹 JS代码
我们介绍一个可兼容所有浏览器的加入收藏代码代码,大概原理是这样的我们根据获取用户navigator.userAgent.toLowerCase()信息来判断浏览器,根据浏览器是否支持加入收藏js命令, ...
- js 代码记录
window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...
- python json string和dict的转化
__author__ = 'SRC_TJ_XiaoqingZhang' import json data1 = {'b': 789, 'c': 456, 'a': 123} encode_json = ...