2009-09-12 30 views

回答

4

我有使用Apache Commons HttpClient庫來執行此操作。看看這裏: http://hc.apache.org/httpclient-3.x/tutorial.html

它比JDK HTTP客戶端支持功能更豐富。

+1

*更新*。從Apache Commons HttpClient主頁引用:「Commons HttpClient項目現在已經結束了,並且不再被開發。**已經被[Apache HttpComponents](http://hc.apache)取代**。 org /)項目[HttpClient](http://hc.apache.org/httpcomponents-client-ga)和[HttpCore](http://hc.apache.org/httpcomponents-core-ga/)模塊,它提供了更好的性能和更大的靈活性。「 – informatik01 2014-01-09 16:49:05

1

如果你所需要的只是讀取url,你不需要求助於第三方庫,java已經內置支持來檢索URL。


import java.net.*; 
import java.io.*; 

public class URLConnectionReader { 
    public static void main(String[] args) throws Exception { 
     URL yahoo = new URL("http://www.yahoo.com/"); 
     URLConnection yc = yahoo.openConnection(); 
     BufferedReader in = new BufferedReader(
           new InputStreamReader(
           yc.getInputStream())); 
     String inputLine; 

     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 
    } 
} 
0

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection;

公共類URLConetent { 公共靜態無效的主要(字串[] args){

URL url; 

    try { 
     // get URL content 

     String a="http://localhost:8080//TestWeb/index.jsp"; 
     url = new URL(a); 
     URLConnection conn = url.openConnection(); 

     // open the stream and put it into BufferedReader 
     BufferedReader br = new BufferedReader(
          new InputStreamReader(conn.getInputStream())); 

     String inputLine; 
     while ((inputLine = br.readLine()) != null) { 
       System.out.println(inputLine); 
     } 
     br.close(); 

     System.out.println("Done"); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

}

相關問題