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
華夢行 閱讀(980)
評論(0) 編輯 收藏