這是我最近研究出來的結果分享給大家看一下^^
片段程式為
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class WebPageReaders {
private static URLConnection connection;
private static void connect( String urlString ) {
try {
URL url = new URL(urlString);
connection = url.openConnection();
System.out.println(connection.getClass());
//connection.connect();
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readContents() {
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
String htmlstr="";
int wordcount=0;
while (
(inputLine = in.readLine()) != null) {
System.out.println(inputLine);
htmlstr=htmlstr+inputLine;
wordcount = wordcount +inputLine.length();
}
System.out.println("wordcount:"+wordcount);
//System.out.println("htmlstr:\n"+htmlstr);
System.out.println("htmlstrcount:"+htmlstr.length());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
connect("http://tw.search.yahoo.com/");
readContents();
}
}
- Sep 20 Wed 2006 10:56
利用HttpUrlConnection取得yahoo網頁內容^^
全站熱搜
留言列表
發表留言