您的位置:首页 > 理论基础 > 计算机网络

java连接带有密码的https的例子

2017-11-02 16:51 169 查看
因为手上项目需要,之前对接另外一个系统用的是http请求webservice服务,测试环境一切正常,突然生产环境就访问不上,原来是人家生产环境用的https协议,没有导入人家的公钥就无法对接接口

经过网上查询很多资料,解决了自己碰上的问题 java无需ssl(数字证书) 访问https服务的 带用户名与密码的方式 下面是我的源码 写下此篇 方便后面的人使用

PS:注意URL那里 时间有点久 不知道当时为啥这么写了

package com.ab.services.xfire.sap.util;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.net.URL;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

public class HttpsClient {

public static String sendNotice(String url, String username,

String password, String data) {

String result = null;

try {
// 设置SSLContext
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { myX509TrustManager },
null);

// 打开连接
// URL requestUrl = new URL(url);
URL requestUrl = new URL(null, url,
new sun.net.www.protocol.https.Handler());
HttpsURLConnection httpsConn = (HttpsURLConnection) requestUrl
.openConnection();

httpsConn.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
String input = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(input
.getBytes());
httpsConn.setRequestProperty("Authorization", "Basic " + encoding);
// 设置套接工厂
httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory());

// 加入数据
httpsConn.setRequestMethod("POST");
httpsConn.setDoOutput(true);
httpsConn.setDoInput(true);
httpsConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
DataOutputStream out = new DataOutputStream(
httpsConn.getOutputStream());
if (data != null)
out.writeBytes(data);

out.flush();
out.close();

// 获取输入流
BufferedReader in = new BufferedReader(new InputStreamReader(
httpsConn.getInputStream()));
int code = httpsConn.getResponseCode();
if (HttpsURLConnection.HTTP_OK == code) {
String temp = in.readLine();
/* 连接成一个字符串 */
while (temp != null) {
if (result != null)
result += temp;
else
result = temp;
temp = in.readLine();
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
byte[] data1 = result.getBytes();
result =new String(data1,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("=======================================================================");
System.out.println("最终返回的result:"+result);
System.out.println("=======================================================================");
return result;
}

private static TrustManager myX509TrustManager = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}

public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
};


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: