使用HttpURLConnection訪問(wèn)web頁(yè)面 - ┽JAVASE進(jìn)階┾ - Java技術(shù)論壇|JavaSE|JavaEE|SCJP|SCWCD|SCEA|SCDJWS|SCBCD|java論壇 邁勝 - Powered by Discuz!
URLConnection類給應(yīng)用程序和web資源之間架設(shè)起了通信的橋梁,這些web資源通常是通過(guò) url來(lái)標(biāo)記的,比如http://java.sun.com。 本文將講述如何使用HttpURLConnection來(lái)訪問(wèn)web頁(yè)面。 URLConnection是個(gè)抽象類,它有兩個(gè)直接子類分別是HttpURLConnection和JarURLConnection。另外一個(gè)重要的 類是URL,通常URL可以通過(guò)傳給構(gòu)造器一個(gè)String類型的參數(shù)來(lái)生成一個(gè)指向特定地址的URL實(shí)例。比如: URL url = new URL("http://www.j2medev.com"); URLConnection con = url.openConnection(); 通過(guò)上面的語(yǔ)句我們就可以得到一個(gè)URLConnection的實(shí)例,如果你在后面添加一句話 System.out.println(con.getClass())你會(huì)得到class sun.net.www.protocol.http.HttpURLConnection 的輸出,這證明返回來(lái)得con是URLConnection的子類HttpURLConnection實(shí)例。如果你的URL的String參數(shù)是https://java.sun.com,那么它 會(huì)打印出class sun.net.www.protocol.https.HttpsURLConnectionImpl。 下面我們編寫一段程序,通過(guò)使用HttpURLConnection訪問(wèn)web頁(yè)面并把得到的內(nèi)容打印到控制臺(tá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 "
- + "<url>");
- System.exit(0);
- }
- connect(args[0]);
- readContents();
- }
- }
復(fù)制代碼 我 們編譯這個(gè)代碼并執(zhí)行 javac WebPageReader.java java WebPageReader http://localhost 你可以從控制臺(tái)看到他把頁(yè)面的內(nèi)容。 如果你使用代理訪問(wèn)外面的網(wǎng)絡(luò)的話可以在程序中添加上下面的代碼 System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", "10.154.134.110"); System.getProperties().put("proxyPort", "8080"); 下面簡(jiǎn)單的介紹一下重定向的問(wèn)題,當(dāng)你訪問(wèn)某個(gè)URL的時(shí)候,Server可能會(huì)把你重新定向到另外一個(gè)地址,我們可以用程序看看這個(gè)問(wèn)題是怎么發(fā)生的。 我們準(zhǔn)備一個(gè)簡(jiǎn)單的asp頁(yè)面位于http://localhost/test/redirect.asp,內(nèi)容如下 <% response.redirect("http://localhost") %> 當(dāng)訪問(wèn)他的時(shí)候他會(huì)把你定向到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 "
- + "<url>");
- 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
復(fù)制代碼 |
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。