HttpClient_httpclient中使用HTTPS的方法

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;

import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.ClientParamsStack;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.params.HttpParams;

public class HttpClientTest {

    public static void main(String args[]) {

        try {

            HttpClient httpclient = new DefaultHttpClient();
                        //Secure Protocol implementation.
            SSLContext ctx = SSLContext.getInstance("SSL");
                        //Implementation of a trust manager for X509 certificates
            X509TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] xcs,
                        String string) throws CertificateException {

                }

                public void checkServerTrusted(X509Certificate[] xcs,
                        String string) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);

            ClientConnectionManager ccm = httpclient.getConnectionManager();
                        //register https protocol in httpclient‘s scheme registry
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", 443, ssf));

            HttpGet httpget = new HttpGet("");
            HttpParams params = httpclient.getParams();

            params.setParameter("param1", "paramValue1");

            httpget.setParams(params);
            System.out.println("REQUEST:" + httpget.getURI());
            ResponseHandler responseHandler = new BasicResponseHandler();
            String responseBody;

            responseBody = httpclient.execute(httpget, responseHandler);

            System.out.println(responseBody);

            // Create a response handler

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }
}
时间: 2024-08-21 13:04:54

HttpClient_httpclient中使用HTTPS的方法的相关文章

简述Java中Http/Https请求监听方法

一.工欲善其事必先利其器 做Web开发的人总免不了与Http/Https请求打交道,很多时候我们都希望能够直观的的看到我们发送的请求参数和服务器返回的响应信息,这个时候就需要借助于某些工具啦.本文将采用Fiddler2作为分析工具,Fiddler很强大,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据,是越墙抓包之利器.关于工具的介绍可以参考下面的链接: http://www.cnblogs.com/TankXiao/archive/2012/02

haproxy代理https配置方法

记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法: haproxy代理https有两种方式:1)haproxy服务器本身提供ssl证书,后面的web服务器走正常的http 2)haproxy服务器本身只提供代理,后面的web服务器走https(配置ssl证书) 第一种方式:haproxy服务器本身提供ssl证书 注意:需要编译haproxy的时候支持ssl编译参数: #make TARGET=linux26 USE_OPENSSL=

数据仓库建设中的数据建模方法(转)

简介: 本文的主要内容不是介绍现有的比较流行的主要行业的一些数据模型,而是将笔者在数据仓库建设项目中的一些经验,在这里分享给大家.希望帮助大家在数据仓库项目建设中总结出一套能够合乎目前业界规范的,满足大部分行业数据仓库建设标准的一种方法. 所谓水无定势,兵无常法.不同的行业,有不同行业的特点,因此,从业务角度看,其相应的数据模型是千差万别的.目前业界较为主流的是数据仓库厂商主要是 IBM 和 NCR,这两家公司的除了能够提供较为强大的数据仓库平台之外,也有各自的针对某个行业的数据模型. 例如,在

如何在 IIS 中设置 HTTPS 服务

Windows Server2008.IIS7启用CA认证及证书制作完整过程 这篇文章介绍了如何安装证书申请工具: 如何在iis创建证书申请: 如何使用iis申请证书生成的txt文件,在工具中开始申请证书: 如何导出证书: 以及在网站中开始使用https的方法:

ios 中使用https的知识

先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/ ios中使用https,主要就是使用NSURLCredential,先看些必要的官方文档: NSURLCredential is an immutable object representing an authentication credential consisting of authentication information specifi

Eclipse中没有andriod问题解决方法

按照网上教程<andriod+环境搭建_图文版>进行安装android,结果,在eclipse中window->preference下找不到"Android"选项,后来查了很久,原来教程并没有让讲安装ADT,ADT是Android Development Tools的缩写,它是集成到Eclipse上的一个插件,能够提供一个强大的集成开发环境来开发Android应用.教程中略掉这一插件的安装了.安装方法: 启动 Eclipse,然后选择 Help > Instal

在asp.net 中生成PDF的方法

近期要用asp.net 2.0生成PDF,看了下书,查了下资料,发现可以有组件帮得上忙,可以下载itextsharp(https://sourceforge.net/projects/itextsharp)下载,然后在工程中引用该控件,举例子如下 1  datatable 的内容转换为PDF      首先,建立一个datatable转换为pdf的方法如下 using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf;using

在 ASP.NET MVC 中使用 HTTPS (SSL/TLS)

某些安全性较高的网页,如网上支付或用户登陆页面,可能会使用到https(SSL/TLS)来提高安全性.本文介绍了如何在ASP.NET MVC中强制某action使用https和如何进行向https页面的跳转.我们先实现强制一个action使用https.这里写了一个RequireHttpsAttribute,它的作用是将非https连接转换成https连接,这样所有使用了RequireHttps这个filter的controller都会强制使用https连接. 1 using System.We

CAS SSO:汇集配置过程中的错误解决方法

本教程为gevin.me原创文章,转载请注明: CAS SSO:配置过程中的错误解决方法 | Gevin’s Blog 本文将收集在配置CAS SSO遇到的所有错误,希望对大家有帮助,也方便下次搭建的时候可以参考! 一.java.lang.exception connector attribute sslcertificatefile must be defined when using ssl with apr Tomcat启动报如上错误,只需要将conf\server.xml里面的以下内容注