代码之家  ›  专栏  ›  技术社区  ›  Arthur Ronald

如何使用Java从服务器端的特定URL获取HTML内容?

  •  1
  • Arthur Ronald  · 技术社区  · 16 年前

    4 回复  |  直到 12 年前
        1
  •  4
  •   David Tinker    16 年前

    我使用了Apache Commons HttpClient库来实现这一点。请看这里: http://hc.apache.org/httpclient-3.x/tutorial.html

    它比JDK HTTP客户端支持功能更丰富。

        2
  •  1
  •   Hamza Yerlikaya    16 年前

    
    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();
        }
    }
    
        3
  •  0
  •   Community Mohan Dere    9 年前

    如果是php,你可以使用 cURL ,但由于它是java,您将使用 HttpURLConnection 正如我刚刚在这个问题上发现的:

    cURL equivalent in JAVA

        4
  •  0
  •   Vaibs    13 年前

    导入java.io.BufferedReader; 导入java.io.IOException; 导入java.io.InputStreamReader; 导入java.net。URL; 导入java.net。URL连接;

    公共类URLCoin{ public静态void main(String[]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();
        }
    
    }
    

    }

    推荐文章