java send https request small instance

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


       /**
	 * 發送https請求
	 * @param jsonParams
	 * @return
	 * @throws KeyStoreException 
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws CertificateException 
	 * @throws NoSuchAlgorithmException 
	 * @throws UnrecoverableKeyException 
	 * @throws KeyManagementException 
	 */
	@SuppressWarnings("deprecation")
	public static String  HttpsProcess(String httpsUrl , String keyStorePath , String keyPass , String jsonParams) throws KeyStoreException, NoSuchAlg         orithmException, CertificateException, FileNotFoundException, IOException, KeyManagementException, UnrecoverableKeyException {
	
	   logger.info("param httpsUrl:"+httpsUrl);  //url  記得用https://ip:8443/***
	   logger.info("param keyStorePath:"+keyStorePath);  //keyStorePath 將server端(接受https請求的web server)的ssh key 文件存放到本地(該類文件存在的地方)該keyStorePath 爲存放路徑 
	   logger.info("param keyPass:"+"**********"); //server 端生成  ssh key 的password
	   logger.info("param jsonParams:"+jsonParams);  // 要發送的數據
	   if(StringUtils.isNullOrEmpty(httpsUrl) || 
		   StringUtils.isNullOrEmpty(keyStorePath) || 
		   StringUtils.isNullOrEmpty(keyPass) || 
		   StringUtils.isNullOrEmpty(jsonParams))
	   {
		   logger.info("同步數據參數爲NULL");
		   logger.error("同步數據參數爲NULL");
		   return Constant.PARAMETER_ERROR_CODE;
	   }
       org.apache.http.client.HttpClient hc = new org.apache.http.impl.client.DefaultHttpClient();  
       java.security.KeyStore trustStore = java.security.KeyStore.getInstance(java.security.KeyStore.getDefaultType());  
       //"123456"爲製作證書時的密碼  
       trustStore.load(new FileInputStream(new File(keyStorePath)), keyPass.toCharArray());
       org.apache.http.conn.ssl.SSLSocketFactory socketFactory = new org.apache.http.conn.ssl.SSLSocketFactory(trustStore);  
       //不校驗域名  
       socketFactory.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
       //這個8446是和被訪問端約定的端口,一般爲443  
       org.apache.http.conn.scheme.Scheme sch = new org.apache.http.conn.scheme.Scheme("https", socketFactory, 8443);  
       hc.getConnectionManager().getSchemeRegistry().register(sch);  
       org.apache.http.client.methods.HttpPost httpost = new org.apache.http.client.methods.HttpPost(httpsUrl);  
       httpost.addHeader(HTTP.CONTENT_TYPE, "application/json");
       httpost.setEntity(new StringEntity(jsonParams, HTTP.UTF_8 ));
       org.apache.http.HttpResponse hres = hc.execute(httpost);  
       org.apache.http.HttpEntity entity = hres.getEntity();  
       String result = EntityUtils.toString(entity);
       
       result = result != null ? result.replace("\"", "") : result;
       return result;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章