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

前端 未结 2 1404
后悔当初
后悔当初 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
    
    0 讨论(0)
  • 2020-12-02 22:00

    The easiest way to do a GET is to use the built in java.net.URL. However, as mentioned, httpclient is the proper way to go, as it will allow you among others to handle redirects.

    For parsing the html, you can use html parser.

    0 讨论(0)
提交回复
热议问题