您现在的位置:首页 >> 基础 >> Java编程 >> 内容

用httpClient访问https--Java学习网

时间:2016-8-8 12:52:29

  核心提示:说明:利用官方的一个例子来说明:Code:httpClientCode:publicclassClientCustomSSL {publicfinalstaticvoidmain(String[] a...

说明:利用官方的一个例子来说明:

Code:


httpClientCode:

publicclassClientCustomSSL {

   publicfinalstaticvoidmain(String[] args)throwsException {

        DefaultHttpClient httpclient =newDefaultHttpClient();

       try{

            KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());

                     FileInputStream instream =

newFileInputStream(newFile("d:\\tomcat.keystore"));

           try{

                    //加载keyStore d:\\tomcat.keystore

                trustStore.load(instream,"123456".toCharArray());

            }finally{

               try{ instream.close(); }catch(Exception ignore) {}

            }

                    //穿件Socket工厂,将trustStore注入

            SSLSocketFactory socketFactory =newSSLSocketFactory(trustStore);

                    //创建Scheme

            Scheme sch =newScheme("https", 8443, socketFactory);

                    //注册Scheme

            httpclient.getConnectionManager().getSchemeRegistry().register(sch);

           //创建http请求(get方式)

                                       HttpGet httpget =

                                        newHttpGet("https://localhost:8443/myDemo/Ajax/serivceJ.action");

            System.out.println("executing request"+ httpget.getRequestLine());

            HttpResponse response = httpclient.execute(httpget);

            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");

            System.out.println(response.getStatusLine());

           if(entity !=null) {

                    System.out.println("Response content length: "+

entity.getContentLength());

              String ss = EntityUtils.toString(entity);

                     System.out.println(ss);

                       EntityUtils.consume(entity);

            }

        }finally{

            httpclient.getConnectionManager().shutdown();

        }

    }

}

 

 

服务器端的action:

 

publicvoidserivceJ() {

       try{

          HttpServletResponse response =

ServletActionContext.getResponse();  

       System.out.println("request...serivceJ");

        response.setCharacterEncoding("UTF-8");

        response.getWriter().write("hello world!");

       }catch(IOException e) {

           e.printStackTrace();

       }

    }

 

 

运行程序:

 

executing requestGET https://localhost:8443/myDemo/Ajax/serivceJ.action HTTP/1.1

----------------------------------------

HTTP/1.1 200 OK

Response content length: 12

hello world!

 

 

到此,一个完整的httpClient访问https的流程就走OK了;

 

注意:生成keyStore的jdk和myEclipse的jdk要一致.否则可能出现错误;


Java免费学习   Java自学网 http://www.javalearns.com

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java

Tags:访问 学习 
作者:不详 来源:网络
    你是从哪里知道本网站的?
  • 网友介绍的
  • 百度搜索的
  • Google搜索的
  • 其它搜索过来的
  • 网址输错了进来的
  • 太忙了不记得了
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • java学习网(www.javalearns.com) © 2014 版权所有 All Rights Reserved.
  • Email:javalearns@163.com 站长QQ:1356121699 晋ICP备14003680号-3
  • java学习网部分内容来自网络或网友发布,如侵犯了您利益,请发邮件至:javalearns@126.com,我们尽快处理!
  • Java学习网
  • 网站统计
  • 晋公网安备 14042902000001号