调用第三方接口出现javax.net.ssl.SSLException: Received fatal alert: protocol_version错误

https的请求需要加上ssl的验证,但是代码里一般只用到http,即使用到https也只是换了个前缀,并没有带上ssl验证的过程。因此解决办法可以是加上SSL验证,也可以是跳过SSL验证,在这个功能开发中采用的是跳过SSL验证,跳过SSL验证时,JDK最好是1.8版本,不然跳不过去。其中涉及到了编码转换问题导致最终得到的结果不是所期望的,解决办法:在被调用的方法中要 StringEntity se = new StringEntity(jsonstr,“UTF-8”);

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请求的HttpClient
@SuppressWarnings("deprecation")
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));
    }
}
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
 * 利用HttpClient进行post请求的工具类
 * @ClassName: HttpClientUtil 
 * @Description: TODO
 * @author Devin  
 * @date 2017年2月7日 下午1:43:38 
 *  
 */
public class HttpClientUtil {
    @SuppressWarnings("resource")
    public static String doPost(String url,String jsonstr,String charset){
    	System.out.println(System.getProperty("java.version"));
    	System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json");
  /*有其他的写成StringEntity se = new StringEntity(jsonstr);
但是我在调用的时候采用上面的写法导致了返回结果有问题,采用下面的没问题,
*/
            StringEntity se = new StringEntity(jsonstr,"UTF-8");
            se.setContentType("application/json");
            //se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
            httpPost.setEntity(se);
            
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
}

调用

 public static void main(String[] args) {
		JSONObject container1 = new JSONObject();
		String url = "XXXXX";
		  container1.put("参数1", "值1");
		   container1.put("参数2", "值2");
		  container1.put("参数3", "值3");
		  System.out.println(container1);
		  System.out.println(container1.toString());		  
		  String httpOrgCreateTestRtn = HttpClientUtil.doPost(url, container1.toString(), "utf-8");
		  JSONObject a = JSONObject.fromObject(httpOrgCreateTestRtn);
		  System.out.println(a.get("参数3").toString());
	}

貌似只支持1.8的,1.7JDK不支持

在myeclipse中,jdk要干净些,在这次试用中,由于没有把myeclipse自带的JDK移除出去,也导致了一些问题

你可能感兴趣的:(调用第三方接口出现javax.net.ssl.SSLException: Received fatal alert: protocol_version错误)