问题
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