溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            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");
                
          //發(fā)送請求
                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 雪山飛鵠 閱讀(24680) 評論(3)  編輯  收藏 所屬分類: javase

          Feedback

          # re: HttpClient請求Https協(xié)議 2013-05-24 21:43 d
          dddd  回復(fù)  更多評論
            

          # re: HttpClient請求Https協(xié)議 2013-07-03 16:51 寶少
          好,可惜看不懂  回復(fù)  更多評論
            

          # re: HttpClient請求Https協(xié)議[未登錄] 2013-08-05 11:28 blue

          求指導(dǎo),代碼的這行編譯錯誤[ Scheme sch = new Scheme("https", 800, socketFactory);]
          The constructor Scheme(String, int, SSLSocketFactory) is undefined

          import org.apache.http.conn.scheme.Scheme;并沒有報錯  回復(fù)  更多評論
            

          主站蜘蛛池模板: 乃东县| 周至县| 新竹县| 芒康县| 阜平县| 临西县| 佛坪县| 聊城市| 子洲县| 衡水市| 奉新县| 金沙县| 秦安县| 鞍山市| 衡阳县| 武陟县| 彭州市| 平南县| 绵竹市| 聂拉木县| 陆良县| 攀枝花市| 独山县| 来凤县| 阜阳市| 专栏| 洱源县| 清远市| 大同县| 定安县| 瓦房店市| 花莲市| 娄底市| 隆化县| 镇远县| 鹿泉市| 六安市| 孝昌县| 聂拉木县| 南和县| 辽阳县|