温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
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
posted on 2011-07-08 14:14 雪山飞鹄 阅读(24653) 评论(3)  编辑  收藏 所属分类: javase

Feedback

# re: HttpClient请求Https协议 2013-05-24 21:43 d
dddd  回复  更多评论
  

# re: HttpClient请求Https协议 2013-07-03 16:51 宝少
好,可惜看不懂  回复  更多评论
  

# re: HttpClient请求Https协议[未登录] 2013-08-05 11:28 blue

求指导,代码的这行编译错误[ Scheme sch = new Scheme("https", 800, socketFactory);]
The constructor Scheme(String, int, SSLSocketFactory) is undefined

import org.apache.http.conn.scheme.Scheme;并没有报错  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: