问题描述
如何在Java中发送异步HTTP GET/POST请求而不等待/读取响应?我不想使用任何第三方库..
How to send asynchronous HTTP GET/POST request in java without waiting/reading response ?I don't want to use any third party libraries ..
推荐答案
如果您根本不希望阅读响应 ,则可以使用 URL.openStream()创建连接,然后立即关闭套接字(或者,如果您觉得对服务器很卑鄙,请忽略它并使其超时).这并不是严格异步的,但是它将比任何依赖于获取和解析服务器响应的方法都快很多.
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.
当然,可以通过手动或使用 java.util.concurrent
中可用的实用程序将 openStream()
调用卸载到另一个线程来使其异步.
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中的异步HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!