隨筆-295  評論-26  文章-1  trackbacks-0

          import java.io.InputStreamReader;
          import java.net.HttpURLConnection;
          import java.net.URL;
          import java.net.URLConnection;
          /*
          ?* hello.java
          ?*
          ?* Created on 2007年8月3日, 上午11:15
          ?*
          ?* To change this template, choose Tools | Template Manager
          ?* and open the template in the editor.
          ?*/

          /**
          ?*
          ?* @author ljl
          ?*/
          public class hello {
          ???
          ??? /** Creates a new instance of hello */
          ??? public hello() {
          ??? }
          ??? public static void main(String s[]) throws Exception{
          ??????? String sss="Sogood";
          ?????? // System.out.print(sss.substring(0,4));
          ??????? ReadURL("??? }
          ??? public static void ReadURL(String URLName) throws Exception // 如果發生異常則向上拋出
          ??? {
          ??????? int HttpResult; // 服務器返回的狀態
          ??????? URL url =new URL(URLName); // 創建URL
          ???????
          ??????? URLConnection urlconn = url.openConnection(); // 試圖連接并取得返回狀態碼
          ??????? urlconn.connect();
          ??????? HttpURLConnection httpconn =(HttpURLConnection)urlconn;
          ??????? HttpResult = httpconn.getResponseCode();
          ??????? System.out.println(HttpResult);
          ??????? if(HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK說明連接不成功
          ?????????? System.out.print("fail");
          ??????? else {
          ??????????? int filesize = urlconn.getContentLength(); // 取數據長度
          ??????????? System.out.println(filesize);
          ??????????? InputStreamReader isReader = new InputStreamReader(urlconn.getInputStream());
          ??????????? char[] buffer = new char[2048]; // 創建存放輸入流的緩沖
          ??????????? int num = 0; // 讀入的字節數
          ??????????? while(num>-1) {
          ??????????????? num = isReader.read(buffer); // 讀入到緩沖區
          ??????????????? if(num < 0) break; // 已經讀完
          ??????????????? System.out.print("suc");
          ????????????? System.out.println(new String(buffer,0,num)); // 顯示出來
          ??????????? }
          ??????????? isReader.close();//關閉輸入流
          ??????? }
          ??? }
          ???
          ???
          }



          大盤預測 國富論
          posted on 2007-08-21 10:38 華夢行 閱讀(982) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 宁德市| 通海县| 平阳县| 萨嘎县| 湖南省| 年辖:市辖区| 子长县| 桐城市| 辽阳市| 怀化市| 台中市| 灌南县| 烟台市| 兰考县| 怀集县| 苍溪县| 化隆| 交城县| 靖宇县| 方山县| 大同市| 济阳县| 章丘市| 呼和浩特市| 揭东县| 连江县| 哈巴河县| 抚宁县| 雅安市| 新沂市| 科尔| 博白县| 孙吴县| 长武县| 侯马市| 汶上县| 沙湾县| 赤城县| 如东县| 洞头县| 麻城市|