<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無(wú)為

    無(wú)為則可為,無(wú)為則至深!

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
    這是<<java網(wǎng)絡(luò)編程>> 中客戶端的例子
    這本書(shū)里還有個(gè)服務(wù)器端的例子
    不過(guò)太長(zhǎng)了,
    // URLGrab.java

    import java.io.*;
    import java.net.*;
    import javax.security.cert.Certificate;
    import com.sun.net.ssl.HttpsURLConnection;   // J2SDK 1.3

    // import java.net.ssl.HttpsURLConnection;      // J2SDK 1.4

    /**
     * A simple application that grabs the contents of an URL and dumps
     * them to a file. The URL may be a https URL. Some properties of the
     * connection are displayed to standard output.
     */
    public class URLGrab {

      /**
       * The location of the https protocol handler
       */
      private static final String PROTOCOL_HANDLERS = 
        "com.sun.net.ssl.internal.www.protocol";

      /**
       * Retrieves the contents and other information about a connection given
       * on the command line.
       * @param args the command line arguments
       * @throws Exception if something went wrong
       */
      public static void main(String args[]) throws Exception {
        System.setProperty("java.protocol.handler.pkgs", PROTOCOL_HANDLERS);

        if (args.length != 2) {
          System.out.println("Please give an URL and a filename.");
        } else {
          URLConnection urlConn = new URL(args[0]).openConnection();
          urlConn.connect();                 // connect to the server
          displayProperties(urlConn);        // display connection properties
          writeContents(urlConn, args[1]);   // write URL contents to file
        } 
      } 

      /**
       * Writes the contents of the URL to a file
       * @param urlConn The connection
       * @param filename The name of the file to write to
       * @throws IOException if a network or other I/O error occurred
       */
      private static void writeContents(URLConnection urlConn, 
                                        String filename) throws IOException {
        InputStream in = urlConn.getInputStream();
        OutputStream out = new FileOutputStream(filename);
        try {
          byte[] buffer = new byte[512];
          int bytesRead;
          while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
          } 
        } 
        finally {
          try {
            out.close();
          } catch (Exception e) { /* do nothing */
          } 
          try {
            in.close();
          } catch (Exception e) { /* do nothing */
          } 
        } 
      } 

      /**
       * Displays some URL connection properties
       * @param urlConn The connection
       */
      private static void displayProperties(URLConnection urlConn) {
        System.out.println("Content Length: " + urlConn.getContentLength());
        System.out.println("Content Type: " + urlConn.getContentType());
        System.out.println("Content Encoding: " + urlConn.getContentEncoding());
        if (urlConn instanceof HttpsURLConnection) {
          displaySecureProperties((HttpsURLConnection) urlConn);
        } 
      } 

      /**
       * Displays some https URL connection properties
       * @param urlConn The secure connection
       */
      private static void displaySecureProperties(HttpsURLConnection urlConn) {
        System.out.println("Cipher Suite: " + urlConn.getCipherSuite());
        Certificate[] chain = urlConn.getServerCertificateChain();
        for (int i = 0; chain != null && i < chain.length; i++) {
          System.out.println("Certificate #" + (i + 1) + ":\n" + chain[i]);
        } 
      } 
    }

    我試了一下,工作正常

    凡是有該標(biāo)志的文章,都是該blog博主Caoer(草兒)原創(chuàng),凡是索引、收藏
    、轉(zhuǎn)載請(qǐng)注明來(lái)處和原文作者。非常感謝。

    posted on 2005-12-14 13:06 草兒 閱讀(399) 評(píng)論(0)  編輯  收藏 所屬分類: Java編程經(jīng)驗(yàn)談
    主站蜘蛛池模板: 亚洲成A人片在线观看无码不卡| 中文字幕免费在线播放| 久久精品国产精品亚洲蜜月| 免费高清av一区二区三区| 69视频在线观看免费| 在线免费视频你懂的| 黄色一级视频免费| 亚洲熟女精品中文字幕| 1区1区3区4区产品亚洲| 亚洲av无码无在线观看红杏| 亚洲人午夜射精精品日韩| 又爽又黄无遮挡高清免费视频| 好爽又高潮了毛片免费下载| 亚洲成人在线免费观看| 欧洲人成在线免费| 中文字幕免费视频精品一| 一级毛片免费播放男男| 杨幂最新免费特级毛片| 深夜A级毛片视频免费| 综合偷自拍亚洲乱中文字幕| 亚洲w码欧洲s码免费| 亚洲精品动漫在线| 久久久久亚洲AV无码永不| 久久精品亚洲综合一品| 亚洲国产精品一区第二页| 久久久久久a亚洲欧洲aⅴ| 国产综合亚洲专区在线| 亚洲精品线路一在线观看| 亚洲成av人片不卡无码久久| 免费成人午夜视频| 亚洲国产成人影院播放| 亚洲毛片网址在线观看中文字幕 | 亚洲桃色AV无码| 亚洲国产一成久久精品国产成人综合| 国产小视频免费观看| 国产三级免费观看| 免费不卡中文字幕在线| 亚洲精品国产高清嫩草影院| 一区二区三区亚洲视频| 中文字幕亚洲无线码a| 精品亚洲永久免费精品|