简体   繁体   English

Java中的异步HTTP请求

[英]asynchronous HTTP request in java

如何在不等待/读取响应的情况下在Java中发送异步HTTP GET / POST请求?我不想使用任何第三方库..

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). 如果您根本不希望阅读响应,则可以使用URL.openStream()创建一个连接,然后立即关闭套接字(或者忽略它,让它超时,如果您觉得对服务器有害) )。 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 . 当然,可以通过手动或使用java.util.concurrent提供的实用程序将openStream()调用卸载到另一个线程来使之异步。

java.util.concurrent can be used . 可以使用java.util.concurrent

If you interested is using third party libraries then you might want to take a look at 如果您有兴趣使用第三方库,那么您可能需要看一下

Async Http Client 异步Http客户端

I would suggest using something like the Jetty HttpClient if you don't mind adding the Jetty libraries to your app. 如果您不介意将Jetty库添加到您的应用程序中,我建议使用诸如Jetty HttpClient之类的东西。 Here's a good example from Jetty's wiki page http://wiki.eclipse.org/Jetty/Tutorial/HttpClient . 这是Jetty的Wiki页面http://wiki.eclipse.org/Jetty/Tutorial/HttpClient中的一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM