<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)談
    主站蜘蛛池模板: 亚洲美女免费视频| 国产在线观看免费av站| 青青青国产在线观看免费| 亚洲美女大bbbbbbbbb| 麻花传媒剧在线mv免费观看| 亚洲人成在久久综合网站| 又粗又大又黑又长的免费视频| 亚洲综合色丁香麻豆| 男男AV纯肉无码免费播放无码| 亚洲中文字幕久久久一区| 妞干网在线免费视频| 国产亚洲女在线线精品| 亚洲区不卡顿区在线观看| 特级做A爰片毛片免费看无码| 亚洲日本va午夜中文字幕一区| 67194熟妇在线永久免费观看| 亚洲色无码专区一区| 免费人成激情视频| 成人免费无码H在线观看不卡| 亚洲成人在线电影| 国产成人免费网站| 五级黄18以上免费看| 亚洲AV无码一区二区乱子伦| 91麻豆最新在线人成免费观看| 亚洲爆乳少妇无码激情| 亚洲色欲久久久久综合网| 在线免费中文字幕| 精品久久久久亚洲| 亚洲成a人片77777kkkk| 亚洲免费网站观看视频| 黄色三级三级三级免费看| 亚洲av无码一区二区三区不卡 | 亚洲中文字幕久久精品蜜桃| 亚洲男人av香蕉爽爽爽爽| 日韩在线不卡免费视频一区| 亚洲欧洲av综合色无码| 国产国拍亚洲精品福利| 国产免费丝袜调教视频| XXX2高清在线观看免费视频| 亚洲人成激情在线播放| 精品亚洲一区二区三区在线播放 |