发送https忽略证书和用户验证

之前和别的公司平台做对接时,使用了http,现在要求改为使用https来请求,这就需要我们修改请求方法,因为对方平台为提供证书,所以我们在访问的时候采用了忽略证书并忽略用户的方式
在开发过程中主要用到了httpclient.jar和httpcore.jar两个jar,我们的平台使用的版本比较老,好像是4.2版本的
首先我们要实现X509TrustManager,重写其方法,使我们在调用的时候忽略证书

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
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;


public class test implements X509TrustManager{

	public static void main(String[] args) {
		 String postVal = "{\"PLAN_DATE\":\"2018-09-26\",\"MILESTONE_NAME\":\"DELIVERY STEAM TURBINE 1\"," +
			   		" \"MILESTONE_CODE\":\"PMON---063\",\"REPORT_TIME\":\"2018-11-13\", \"ORG_CODE\":\"\",\"PROJECT_CODE\":\"\"," +
			   		" \"UPDATE_TIME\":\"2018-11-13\",\"IS_PLAN_DATE_CHANGE\":\"1\",\"COMPLETE_DATE\":\"\"}";
		String url = "https://xxx.xxx.xxx.xxx:xxxx/milestone";
		url = "https://blog.csdn.net/";
		  String s=httpsRequest(url,"POST",postVal);  
		  System.out.println(s);  
	}

	@Override
	public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
	}
	@Override
	public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
	}
		//重写,直接ruturn null,忽略证书
	@Override
	public X509Certificate[] getAcceptedIssuers() {
		return null;
	}
	
	 public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){  
	        StringBuffer buffer=null;  
	        try{  
	        //创建SSLContext  
	        SSLContext sslContext=SSLContext.getInstance("TLS");  
	        TrustManager[] tm={new test()};  
	        //初始化  
	        sslContext.init(null, tm, new java.security.SecureRandom());;  
	        //获取SSLSocketFactory对象  
	        SSLSocketFactory ssf=sslContext.getSocketFactory();  

 			 //如果是Tomcat服务器,可直接写,如果是weblogic,用  url = new URL(null,urlString,new sun.net.www.protocol.https.Handler()); 
	        URL url=new URL(requestUrl);  	      
	        HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();  
	        
	        conn.setDoOutput(true);  
	        conn.setDoInput(true);  
	        conn.setUseCaches(false);  
	        conn.setRequestMethod(requestMethod);  
	        //设置忽略用户,如果不设置,请求CSDN,可能会获取不到网页信息,而是其他信息
	        conn.setHostnameVerifier(new HostnameVerifier() {
				@Override
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
	        //设置当前实例使用的SSLSoctetFactory  
	        conn.setSSLSocketFactory(ssf);  
	        conn.connect();  
	        //往服务器端写内容  
	        if(null!=outputStr){  
	            OutputStream os=conn.getOutputStream();  
	            os.write(outputStr.getBytes("utf-8"));  
	            os.close();  
	        }  
	          
	        //读取服务器端返回的内容  
	        InputStream is=conn.getInputStream();  
	        InputStreamReader isr=new InputStreamReader(is,"utf-8");  
	        BufferedReader br=new BufferedReader(isr);  
	        buffer=new StringBuffer();  
	        String line=null;  
	        while((line=br.readLine())!=null){  
	            buffer.append(line);  
	        }  
	        }catch(Exception e){  
	            e.printStackTrace();  
	        }  
	        return buffer.toString();  
	    }  
}

你可能感兴趣的:(java)