赞
踩
本人目前在 开发兼维护兰州大市场的项目 , 由于与另一个系统的接口变化,需使用https 的方式在后台发送 请求。
编写端口 时 遇到的问题 和解决办法同大家分享下
如下是https发送请求的实现代码。
package cn.com.gsjiahua.eaglefortune.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class HttpsUtil {
public static String URL = "https://www.x.cn";
private static class DefaultTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
public static HttpsURLConnection getHttpsURLConnection(String uri) throws IOException {
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SSLSocketFactory ssf = ctx.getSocketFactory();
URL url = new URL(uri);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
return httpsConn;
}
public static String getHttpsResult(String url) {
String result = "";
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(getHttpsURLConnection(url).getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (IOException e) {
System.out.println("发送Https请求出现异常!message:" + e.getMessage()+",url:"+url);
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public static String detect(String uid, String applyId, String pay_time) {
String url = URL+"";
String res = getHttpsResult(url);
return res;
}
}
当将程序部署到weblogic 服务器 时又出现
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。