httpclient中使用HTTPS的方法

本文详细介绍了如何在Java中使用Apache HttpClient库实现HTTPS请求,包括SSL上下文配置、信任管理器实现以及如何注册HTTPS协议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码   收藏代码
  1. import javax.net.ssl.SSLContext;  
  2.   
  3. import javax.net.ssl.TrustManager;  
  4. import javax.net.ssl.X509TrustManager;  
  5. import java.security.cert.CertificateException;  
  6. import java.security.cert.X509Certificate;  
  7. import org.apache.http.client.ClientProtocolException;  
  8. import org.apache.http.client.HttpClient;  
  9. import org.apache.http.client.ResponseHandler;  
  10. import org.apache.http.client.methods.HttpGet;  
  11. import org.apache.http.conn.ClientConnectionManager;  
  12.   
  13. import org.apache.http.conn.scheme.Scheme;  
  14. import org.apache.http.conn.scheme.SchemeRegistry;  
  15. import org.apache.http.conn.scheme.SchemeSocketFactory;  
  16. import org.apache.http.conn.ssl.SSLSocketFactory;  
  17. import org.apache.http.impl.client.BasicResponseHandler;  
  18. import org.apache.http.impl.client.ClientParamsStack;  
  19. import org.apache.http.impl.client.DefaultHttpClient;  
  20. import org.apache.http.params.DefaultedHttpParams;  
  21. import org.apache.http.params.HttpParams;  
  22.   
  23. public class HttpClientTest {  
  24.   
  25.     public static void main(String args[]) {  
  26.   
  27.         try {  
  28.   
  29.             HttpClient httpclient = new DefaultHttpClient();  
  30.                         //Secure Protocol implementation.  
  31.             SSLContext ctx = SSLContext.getInstance("SSL");  
  32.                         //Implementation of a trust manager for X509 certificates  
  33.             X509TrustManager tm = new X509TrustManager() {  
  34.   
  35.                 public void checkClientTrusted(X509Certificate[] xcs,  
  36.                         String string) throws CertificateException {  
  37.   
  38.                 }  
  39.   
  40.                 public void checkServerTrusted(X509Certificate[] xcs,  
  41.                         String string) throws CertificateException {  
  42.                 }  
  43.   
  44.                 public X509Certificate[] getAcceptedIssuers() {  
  45.                     return null;  
  46.                 }  
  47.             };  
  48.             ctx.init(nullnew TrustManager[] { tm }, null);  
  49.             SSLSocketFactory ssf = new SSLSocketFactory(ctx);  
  50.   
  51.             ClientConnectionManager ccm = httpclient.getConnectionManager();  
  52.                         //register https protocol in httpclient's scheme registry  
  53.             SchemeRegistry sr = ccm.getSchemeRegistry();  
  54.             sr.register(new Scheme("https"443, ssf));  
  55.   
  56.             HttpGet httpget = new HttpGet("");  
  57.             HttpParams params = httpclient.getParams();  
  58.   
  59.             params.setParameter("param1""paramValue1");  
  60.   
  61.             httpget.setParams(params);  
  62.             System.out.println("REQUEST:" + httpget.getURI());  
  63.             ResponseHandler responseHandler = new BasicResponseHandler();  
  64.             String responseBody;  
  65.   
  66.             responseBody = httpclient.execute(httpget, responseHandler);  
  67.   
  68.             System.out.println(responseBody);  
  69.   
  70.             // Create a response handler  
  71.   
  72.         } catch (NoSuchAlgorithmException e) {  
  73.             // TODO Auto-generated catch block  
  74.             e.printStackTrace();  
  75.         } catch (ClientProtocolException e) {  
  76.             // TODO Auto-generated catch block  
  77.             e.printStackTrace();  
  78.         } catch (IOException e) {  
  79.             // TODO Auto-generated catch block  
  80.             e.printStackTrace();  
  81.         } catch (Exception ex) {  
  82.             ex.printStackTrace();  
  83.   
  84.         }  
  85.     }  
  86. }  
分享到:   
  • 2012-07-25 08:42
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值