少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          Java HTTPS Client FAQ: Can you share some source code for a Java HTTPS client application?

          Sure, here's the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can't find the source today to give them credit, so my apologies for that.

          I just used this program to troubleshoot a problem with Java and HTTPS URLs, including all that nice Java SSL keystore and cacerts stuff you may run into when working with Java, HTTPS/SSL, and hitting a URL.

          This Java program should work if you are hitting an HTTPS URL that has a valid SSL certificate from someone like Verisign or Thawte, but will not work with other SSL certificates unless you go down the Java keystore road.

          Example Java HTTPS client program

          Here's the source code for my simple Java HTTPS client program:




          package foo;

          import java.net.URL;
          import java.io.*;
          import javax.net.ssl.HttpsURLConnection;

          public class JavaHttpsExample
          {
            public static void main(String[] args)
            throws Exception
            {
              String httpsURL = "https://your.https.url.here/";
              URL myurl = new URL(httpsURL);
              HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
              InputStream ins = con.getInputStream();
              InputStreamReader isr = new InputStreamReader(ins);
              BufferedReader in = new BufferedReader(isr);

              String inputLine;

              while ((inputLine = in.readLine()) != null)
              {
                System.out.println(inputLine);
              }

              in.close();
            }
          }






          Just change the URL shown there to the HTTPS URL you want to access, and hopefully everything will work well for you. (If not, there's always that Comment section down below, lol.)




          http://www.devdaily.com/blog/post/java/simple-https-example
          posted on 2012-08-16 00:41 abin 閱讀(758) 評論(0)  編輯  收藏 所屬分類: httpClient
          主站蜘蛛池模板: 南康市| 富阳市| 江山市| 静海县| 托克托县| 大荔县| 长沙市| 南木林县| 威远县| 集贤县| 上高县| 东阿县| 叶城县| 博湖县| 怀安县| 卓资县| 梅河口市| 大庆市| 临沂市| 永新县| 山东省| 阳高县| 陕西省| 华容县| 桦甸市| 房产| 炎陵县| 临猗县| 隆德县| 瑞金市| 长顺县| 富阳市| 永福县| 南汇区| 达州市| 陇西县| 祁门县| 璧山县| 扎囊县| 治多县| 西乌珠穆沁旗|