整体报错信息为:
java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class com.ibm.websphere.ssl.protocol.SSLSocketFactory
1.检查是否是环境的问题,检查jdk等信息
2.若环境无误,换一种调用方式,代码如下:
package nc.pub.tool.trans;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.OutputStream;
//import java.io.OutputStreamWriter;
//import java.io.PrintWriter;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.net.URLConnection;
//import java.nio.charset.Charset;
import java.nio.charset.Charset;
import nc.vo.pub.BusinessException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
/**
* 调用http接口工具类
*
*/
public class HttpConnection {
/**
* 发送 post请求
*/
@SuppressWarnings("resource")
public String sendPost(String json, String URL) throws BusinessException{
String obj = null;
// 创建默认的httpClient实例.
HttpClient httpclient = null;
// 创建httppost
HttpPost httppost = new HttpPost(URL);
// httppost.addHeader("Content-type", "application/json; charset=utf-8");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("x-zop-ns", "budget");
httppost.setHeader("accept", "*/*");
httppost.setHeader("connection", "Keep-Alive");
httppost.setHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
httppost.setHeader("Content-Type","application/json;charset=utf-8");
try {
httpclient = new SSLClient();
StringEntity s = new StringEntity(json, Charset.forName("UTF-8")); // 对参数进行编码,防止中文乱码
s.setContentEncoding("UTF-8");
httppost.setEntity(s);
HttpResponse response = httpclient.execute(httppost);
// 获取相应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
obj = EntityUtils.toString(entity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(e.getMessage());
}
System.out.println("输出返回信息:" + obj.toString());
return obj.toString();
}
}
package nc.pub.tool.trans;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* 绕过https证书认证的方法
*
*/
public class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
其中导入jar为:
httpcore-4.4.1
httpclient-4.4.1
自己网上下载一下
博客内容讲述了在调用接口时遇到java.net.SocketException,原因是ClassNotFoundException,找不到com.ibm.websphere.ssl.protocol.SSLSocketFactory类。解决方案包括检查环境配置和使用Apache HttpClient绕过SSL证书认证的方法,提供了一个自定义的SSLClient类和相关代码示例。
2831

被折叠的 条评论
为什么被折叠?



