HttpClient請求Https協議

來源:互聯網
上載者:User
package com.ipmotor.sm.db;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * 利用HttpClient,類比https串連
 * 使用4.1版本
 * @since 2011.7.7
 */
public class Test{
    
    /**
     * 運行主方法
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
      //獲得httpclient對象
      HttpClient httpclient = new DefaultHttpClient();
      //獲得密匙庫
      KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
      FileInputStream instream = new FileInputStream(new File("D:/zzaa"));
      //密匙庫的密碼
      trustStore.load(instream, "123456".toCharArray());
      //註冊密匙庫
      SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
      //不校正網域名稱
      socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      Scheme sch = new Scheme("https", 800, socketFactory);
      httpclient.getConnectionManager().getSchemeRegistry().register(sch);
      //獲得HttpGet對象
      HttpGet httpGet = null;
      httpGet = new HttpGet("https://10.15.32.176:800/cgi-bin/service.cgi?session=caef0c3742c8f8ef4c98772e860c9fd2&rand=128&domain=sun.com&type=domain&cmd=disable");
      //發送請求
      HttpResponse response = httpclient.execute(httpGet);
      //輸出傳回值
      InputStream is = response.getEntity().getContent();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      String line = "";
      while((line = br.readLine())!=null){
          System.out.println(line);
      }
    }
}

依賴的jar包
commons-codec-1.4.jar
commons-logging-1.1.1.jar
httpclient-4.1.1.jar
httpclient-cache-4.1.1.jar
httpcore-4.1.jar

httpmime-4.1.1.jar

  轉載請標明出處 http://blog.csdn.net/shimiso 

技術交流群:173711587

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.