How do I retrieve a URL from a web site using Java?

前端 未结 2 1405
后悔当初
后悔当初 2020-12-02 21:00

I want to use HTTP GET and POST commands to retrieve URLs from a website and parse the HTML. How do I do this?

2条回答
  •  抹茶落季
    2020-12-02 21:44

    You can use HttpURLConnection in combination with URL.

    URL url = new URL("http://example.com");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();
    
    InputStream stream = connection.getInputStream();
    // read the contents using an InputStreamReader
    

提交回复
热议问题