2009-09-12 13 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(); 
    } 

} 

}

相关问题