several requests from one HttpURLConnection

后端 未结 2 943
广开言路
广开言路 2021-01-04 02:43

How can I do several request in one HttpURLConnection with Java?

 URL url = new URL(\"http://my.com\");
 HttpURLConnection connection = (HttpURLConnection)ur         


        
相关标签:
2条回答
  • 2021-01-04 03:08

    Beyond the correct answer, maybe what you actually want is reuse of the underlying TCP connection, aka "persistent connections", which are indeed supported by JDK's HttpURLConnection. So you don't need to use other http libs for that reason; although there are other legitimate reason, possibly performance (but not necessarily, depends on use case, library).

    0 讨论(0)
  • 2021-01-04 03:13

    From the Javadoc:

    Each HttpURLConnection instance is used to make a single request.

    The object apparently isn't meant to be re-used.

    Aside from a little memory thrashing and inefficiency, there's no big problem with opening one HttpURLConnection for every request you want to make. If you want efficient network IO on a larger scale, though, you're better off using a specialized library like Apache HttpClient.

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