HttpClient访问单向https(不使用密钥库和信任库)

HttpClient访问单向https(不使用密钥库和信任库)
//客户端代码,用来测试访问Servlet:

package com.abin.lee.ssh.basic.unionpay;

import java.io.BufferedInputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.AllClientPNames;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.junit.Test;

public class GetUnionpayMessages1Test {
 private static final String HTTPURL = "https://124.288.188.28:1443/stsf/deal/unionpay/UnionPayDeal";

 @Test
 public void testGetMessageState() throws Exception {
  HttpClient httpClient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost(HTTPURL);
  httpClient.getParams().setParameter(AllClientPNames.PROTOCOL_VERSION,
    HttpVersion.HTTP_1_1);
  httpClient.getParams().setParameter(
    AllClientPNames.USE_EXPECT_CONTINUE, Boolean.TRUE);
  httpClient.getParams().setParameter(
    AllClientPNames.HTTP_CONTENT_CHARSET, "UTF_8");
  httpClient.getParams().setParameter(
    AllClientPNames.CONN_MANAGER_TIMEOUT, 10000l);
  httpClient.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT,
    2000);
  httpClient.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 10000);
  try {
   TrustManager easyTrustManager = new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] chain,
      String authType) throws CertificateException {
     // 哦,这很简单!
    }
    public void checkServerTrusted(X509Certificate[] chain,
      String authType) throws CertificateException {
      //哦,这很简单!
    }
    public X509Certificate[] getAcceptedIssuers() {
     return null;
    }
   };
   SSLContext sslcontext = SSLContext.getInstance("TLS");
   sslcontext
     .init(null, new TrustManager[] { easyTrustManager }, null);
   SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
   SSLSocket socket = (SSLSocket) sf.createSocket();
   socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });
   HttpParams params = new BasicHttpParams();
   Scheme sch = new Scheme("https", 1443, sf);
   sf.connectSocket(socket, "124.288.188.28", 1443, null, -1, params);

   httpClient.getConnectionManager().getSchemeRegistry().register(sch);

   List<NameValuePair> nvps = new ArrayList<NameValuePair>();
   nvps.add(new BasicNameValuePair("userName", "abin"));
   nvps.add(new BasicNameValuePair("createTime", new SimpleDateFormat(
     "yyyy-MM-dd HH:mm:ss").format(new java.util.Date())));
   HttpResponse httpResponse = null;
   String result = "";
   try {
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    httpResponse = httpClient.execute(httpPost);
    BufferedInputStream buffer = new BufferedInputStream(
      httpResponse.getEntity().getContent());
    byte[] bytes = new byte[1024];
    int line = 0;
    StringBuilder builder = new StringBuilder();
    while ((line = buffer.read(bytes)) != -1) {
     builder.append(new String(bytes, 0, line, "UTF-8"));
    }
    result = builder.toString();
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    if (httpPost.isAborted()) {
     httpPost.abort();
    }
    httpClient.getConnectionManager().shutdown();
   }
   System.out.println("result=" + result);

  } finally {
   // When HttpClient instance is no longer needed,
   // shut down the connection manager to ensure
   // immediate deallocation of all system resources
   httpClient.getConnectionManager().shutdown();
  }
 }
}





//在%TOMCAT_HOME%/conf/server.xml里面添加的内容

<Connector port="1443" protocol="HTTP/1.1" SSLEnabled="true"
     maxThreads="150" scheme="https" secure="true"
     clientAuth="false" keystoreFile="D:/tomcat.keystore"
     keystorePass="longcode" sslProtocol="TLS" />





//UnionPayDeal.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Administrator
 *
 */
public class UnionPayDeal extends HttpServlet {
 private static final long serialVersionUID = -1370581177759574628L;

 public void init(ServletConfig config) throws ServletException {
  super.init(config);
 }

 public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
  System.out.println("接收到银联的异步通知");
  String accept=null;
  BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));
  int num=0;
  char[] buffer=new char[1024];
  while((num=reader.read(buffer))!=-1){
   accept=new String(buffer,0,num);
   System.out.println("accept="+accept);
  }
 }
 
 
 public void destroy() {
  super.destroy();
 }
}


 

你可能感兴趣的:(HttpClient访问单向https(不使用密钥库和信任库))