- 論壇徽章:
- 0
|
URLConnection類給應(yīng)用程序和web資源之間架設(shè)起了通信的橋梁,這些web資源通常是通過url來標(biāo)記的,比如http://java.sun.com。本文將講述如何使用HttpURLConnection來訪問web頁面。
URLConnection是個抽象類,它有兩個直接子類分別是HttpURLConnection和JarURLConnection。另外一個重要的類是URL,通常URL可以通過傳給構(gòu)造器一個String類型的參數(shù)來生成一個指向特定地址的URL實例。比如:
URL url = new URL("http://www.j2medev.com");
URLConnection con = url.openConnection();
通過上面的語句我們就可以得到一個URLConnection的實例,如果你在后面添加一句話System.out.println(con.getClass())你會得到class sun.net.www.protocol.http.HttpURLConnection 的輸出,這證明返回來得con是URLConnection的子類HttpURLConnection實例。如果你的URL的String參數(shù)是https://java.sun.com,那么它會打印出class sun.net.www.protocol.https.HttpsURLConnectionImpl。下面我們編寫一段程序,通過使用HttpURLConnection訪問web頁面并把得到的內(nèi)容打印到控制臺。代碼如下
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 WebPageReader {
private static URLConnection connection;
private static void connect( String urlString ) {
try {
URL url = new URL(urlString);
connection = url.openConnection();
System.out.println(connection.getClass());
} 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;
while (
(inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("usage: java WebPageReader "
+ "");
System.exit(0);
}
connect(args[0]);
readContents();
}
}
我們編譯這個代碼并執(zhí)行
javac WebPageReader.java
java WebPageReader http://localhost 你可以從控制臺看到他把頁面的內(nèi)容。
如果你使用代理訪問外面的網(wǎng)絡(luò)的話可以在程序中添加上下面的代碼
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "10.154.134.110");
System.getProperties().put("proxyPort", "8080");
下面簡單的介紹一下重定向的問題,當(dāng)你訪問某個URL的時候,Server可能會把你重新定向到另外一個地址,我們可以用程序看看這個問題是怎么發(fā)生的。我們準(zhǔn)備一個簡單的asp頁面位于http://localhost/test/redirect.asp,內(nèi)容如下
當(dāng)訪問他的時候他會把你定向到http://localhost 的地址去。
import java.net.URL;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.io.IOException;
public class RedirectingReader {
private static HttpURLConnection connection;
private static void connect( String urlString ) {
try {
URL url = new URL(urlString);
connection
= (HttpURLConnection)url.openConnection();
System.out.println(connection.getURL());
System.out.println(
connection.getResponseCode() +
" " + connection.getResponseMessage());
System.out.println(connection.getURL());
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println(
"usage: java WebPageReader "
+ "");
System.exit(0);
}
connect(args[0]);
}
}
編譯程序并執(zhí)行
javac RedirectingReader.java
java RedirectingReader http://localhost/test/redirect.asp
可以得到輸出為
http://localhost/test/redirect.asp
200 OK
http://localhost/localstart.asp
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/109937/showart_2162619.html |
|