隨筆-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)  編輯  收藏

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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 遂宁市| 江源县| 葵青区| 顺义区| 乌海市| 织金县| 绍兴县| 科尔| 古田县| 独山县| 休宁县| 巫山县| 普陀区| 邵武市| 延边| 司法| 汾阳市| 贵溪市| 鄱阳县| 宜章县| 襄樊市| 武城县| 峡江县| 晋江市| 杭州市| 扎兰屯市| 襄垣县| 林西县| 沙雅县| 略阳县| 遂平县| 衢州市| 子洲县| 天长市| 尼木县| 武邑县| 高雄市| 罗源县| 浦县| 龙岩市| 平南县|