<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
    已經發布的準備要測試的https服務:
    package com.abin.lee.https;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Map;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    @SuppressWarnings("serial")
    public class ReceiveHttpClientRequest extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("receive https request");
    Map map=request.getParameterMap();
    String user=(((Object[])map.get("user"))[0]).toString();
    System.out.println("user="+user);
    String pwd=(((Object[])map.get("pwd"))[0]).toString();
    System.out.println("pwd="+pwd);
    //給調用者返回值
    PrintWriter write=response.getWriter();
    write.write("receive HttpClient success");
    write.flush();
    write.close();
    }
    }





    //web.xml
    <servlet>
    <servlet-name>httpsClientRequest</servlet-name>
    <servlet-class>com.abin.lee.https.ReceiveHttpClientRequest</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>httpsClientRequest</servlet-name>
    <url-pattern>/httpsClientRequest</url-pattern>
    </servlet-mapping>




    //HttpClient測試類
    package com.abin.lee.test;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.security.KeyStore;
    import java.util.ArrayList;
    import java.util.List;
    import javax.net.ssl.KeyManagerFactory;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.TrustManagerFactory;
    import junit.framework.TestCase;
    import org.apache.http.HttpResponse;
    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.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.protocol.HTTP;
    import org.junit.Before;
    import org.junit.Test;
    public class HttpsClient extends TestCase {
    private String httpUrl = "https://localhost:8443/global/httpsClientRequest";
    // 客戶端密鑰庫
    private String sslKeyStorePath;
    private String sslKeyStorePassword;
    private String sslKeyStoreType;
    // 客戶端信任的證書
    private String sslTrustStore;
    private String sslTrustStorePassword;
    @Before
    public void setUp() {
    sslKeyStorePath = "D:\\home\\tomcat.keystore";
    sslKeyStorePassword = "stevenjohn";
    sslKeyStoreType = "JKS"; // 密鑰庫類型,有JKS PKCS12等
    sslTrustStore = "D:\\home\\tomcat.keystore";
    sslTrustStorePassword = "stevenjohn";
    System.setProperty("javax.net.ssl.keyStore", sslKeyStorePath);
    System.setProperty("javax.net.ssl.keyStorePassword",
    sslKeyStorePassword);
    System.setProperty("javax.net.ssl.keyStoreType", sslKeyStoreType);
    // 設置系統參數
    System.setProperty("javax.net.ssl.trustStore", sslTrustStore);
    System.setProperty("javax.net.ssl.trustStorePassword",
    sslTrustStorePassword);
    }
    @Test
    public void testHttpsClient() {
    SSLContext sslContext = null;
    try {
    KeyStore kstore = KeyStore.getInstance("jks");
    kstore.load(new FileInputStream(sslKeyStorePath),
    sslKeyStorePassword.toCharArray());
    KeyManagerFactory keyFactory = KeyManagerFactory
    .getInstance("sunx509");
    keyFactory.init(kstore, sslKeyStorePassword.toCharArray());
    KeyStore tstore = KeyStore.getInstance("jks");
    tstore.load(new FileInputStream(sslTrustStore),
    sslTrustStorePassword.toCharArray());
    TrustManager[] tm;
    TrustManagerFactory tmf = TrustManagerFactory
    .getInstance("sunx509");
    tmf.init(tstore);
    tm = tmf.getTrustManagers();
    sslContext = SSLContext.getInstance("SSL");
    sslContext.init(keyFactory.getKeyManagers(), tm, null);
    } catch (Exception e) {
    e.printStackTrace();
    }
    try {
    HttpClient httpClient = new DefaultHttpClient();
    SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext);
    Scheme sch = new Scheme("https", 8443, socketFactory);
    httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    HttpPost httpPost = new HttpPost(httpUrl);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("user", "abin"));
    nvps.add(new BasicNameValuePair("pwd", "abing"));
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    HttpResponse httpResponse = httpClient.execute(httpPost);
    String spt = System.getProperty("line.separator"); 
    BufferedReader buffer = new BufferedReader(new InputStreamReader(
    httpResponse.getEntity().getContent()));
    StringBuffer stb=new StringBuffer();
    String line=null;
    while((line=buffer.readLine())!=null){
    stb.append(line);
    }
    buffer.close();
    String result=stb.toString();
    System.out.println("result="+result);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }







    //tomcat配置文件:(前提是https雙向驗證證書生成的沒有一點問題)

    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
      SSLEnabled="true" maxThreads="150" scheme="https"
      secure="true" clientAuth="true" sslProtocol="TLS"
      keystoreFile="D:\\home\\tomcat.keystore" keystorePass="stevenjohn"
      truststoreFile="D:\\home\\tomcat.keystore" truststorePass="stevenjohn" />



    posted on 2012-09-27 00:11 abin 閱讀(3202) 評論(0)  編輯  收藏 所屬分類: HTTPS
    主站蜘蛛池模板: 国产亚洲一区二区在线观看| 免费一级肉体全黄毛片| 亚洲国产精品无码AAA片| 一区二区三区视频免费观看| 国产在线观看免费完整版中文版| 亚洲精品无码高潮喷水A片软| 在线播放免费播放av片| 亚洲AV无码XXX麻豆艾秋| 国产一卡二卡≡卡四卡免费乱码| 亚洲AV女人18毛片水真多| 免费看男女下面日出水视频| 人人爽人人爽人人片av免费| 伊人久久大香线蕉亚洲| 久久精品成人免费观看| 亚洲女人影院想要爱| 午夜私人影院免费体验区| 国产成人高清亚洲一区91| 亚洲最大av无码网址| 免费在线看污视频| 亚洲成a人不卡在线观看| 成人五级毛片免费播放| 国产成人亚洲精品91专区高清| 亚洲一区视频在线播放| 免费精品无码AV片在线观看| 久久久久se色偷偷亚洲精品av | 亚洲色欲啪啪久久WWW综合网| 国产自产拍精品视频免费看| 精品国产污污免费网站入口 | jiz zz在亚洲| 亚洲XX00视频| 日本免费人成视频在线观看| 亚洲视频无码高清在线| 亚洲综合区小说区激情区| 84pao国产成视频免费播放| 亚洲乱码一区av春药高潮| 久久不见久久见免费影院| 色婷婷综合缴情综免费观看 | 日韩精品免费一区二区三区| 一区二区3区免费视频| 亚洲国产成人精品电影| 亚洲人成网站18禁止一区|