HttpClient请求Https协议

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值