Sending GET & POST requests in Java or other without responses

泪湿孤枕 提交于 2020-01-14 14:14:29

问题


Is it possible to make GET & POST requests in Java or another language such that you don't care about what is returned?

As in just sending the requests but not wanting to receive any responses?


回答1:


Whether you care about the response or not, it will be sent. The HTTP protocol specifications say that it must be.

If you don't care about the response, your client could just close the connection immediately after sending the request. But the chances are that you do want to know that the request was processed (i.e. the response status) even if you don't want to look at the contents of the response message.
So maybe you could send the request and request body, and read the response status and then close the connection without reading the response body. However, this has a downside. It means that you can't reuse the HTTP connection to make further requests. The next request to the same server has to open a new connection.




回答2:


You could use anynchronous HTTP requests if you don't care about the responses (that way your worker thread will not have to wait for the response to come back). See http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp.html for some details on Asynchronous/Synchronous HTTP queries in Java. Then you can control if the anychronous thread does or does not handle the response (or any failure flagged on the communication) - as long as there were no TCP level failures on the request the connection will still be opened.




回答3:


You can't control whether or not the server returns a response. Your code is free to ignore any response it receives.




回答4:


It's pretty hard to not get responses because they're part of the HTTP protocol. but you can certainly ignore the responses.



来源:https://stackoverflow.com/questions/8612364/sending-get-post-requests-in-java-or-other-without-responses

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