Can I do an http GET and include a body using HttpURLConnection?

随声附和 提交于 2019-12-11 03:56:43

问题


I need to perform an http GET for a REST service and include a body in the GET. Unfortunately, setting #setDoOutput( true ) on the connection forces a POST. Is there anyway to send a GET with a body?

Edit: The body I'm trying to send is JSON.


回答1:


It is not possible to send content for an HTTP GET using HttpURLConnection. By setting setDoOutput(true) on an HttpURLConnection the verb is forced to be POST.

The documentation for the REST API I was using described a JSON body for the endpoint in question, but URL parameters were accepted.




回答2:


It might not be possible through HttpUrlConnection, although you might be able to do it through another APIs BUT, if you have to do it that way chances that you are doing something wrong in your architecture are high because it goes against the basic usage of GET Http Method and different problems might arise like: If you ever try to take advantage of caching, Proxies are not going to look in the GET body to see if the parameters have an impact on the response. It's not a good implementation based on standard practices and it could cause problems with some browsers / services.

Take a look at this question for more information.

HTTP GET with request body

Hope this helps.

Regards!



来源:https://stackoverflow.com/questions/18664413/can-i-do-an-http-get-and-include-a-body-using-httpurlconnection

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