asynchronous HTTP request in java

泄露秘密 提交于 2021-01-27 03:31:51

问题


How to send asynchronous HTTP GET/POST request in java without waiting/reading response ?I don't want to use any third party libraries ..


回答1:


If you're not interested in reading the response at all you can just use URL.openStream() to create a connection and then immediately close the socket (or ignore it and let it time out, if you feel like being mean to the server). This isn't strictly asynchronous, but it will be quite a bit faster than any approach that relies upon fetching and parsing the server's response.

This can of course be made asynchronous by offloading the openStream() calls to another thread, either manually or by using the utilities available in java.util.concurrent.




回答2:


java.util.concurrent can be used .

If you interested is using third party libraries then you might want to take a look at

Async Http Client




回答3:


I would suggest using something like the Jetty HttpClient if you don't mind adding the Jetty libraries to your app. Here's a good example from Jetty's wiki page http://wiki.eclipse.org/Jetty/Tutorial/HttpClient.



来源:https://stackoverflow.com/questions/8983016/asynchronous-http-request-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!