25

Here is my code:

String addr = "http://172.26.41.18:8080/domain/list";

URL url = new URL(addr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setUseCaches(false);
httpCon.setAllowUserInteraction(false);
httpCon.setRequestMethod("GET");
httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");

httpCon.connect();

OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());

System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());

out.close();

What I see in response:

500 Server error

I open my httpCon var, and what I see:

POST /rest/platform/domain/list HTTP/1.1

Why is it set to POST even though I have used httpCon.setRequestMethod("GET"); to set it to GET?

  • Does the basic authentication travel over the wire via POST? – cdeszaq Jan 6 '12 at 15:20
  • Were you testing on a Galaxy Nexus? httpCon.setDoOutput(true); forced POST on my Galaxy Nexus but not on the Galaxy S2 running Gingerbread. – dzeikei Apr 24 '12 at 3:58
58

The httpCon.setDoOutput(true); implicitly set the request method to POST because that's the default method whenever you want to send a request body.

If you want to use GET, remove that line and remove the OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); line. You don't need to send a request body for GET requests.

The following should do for a simple GET request:

String addr = "http://172.26.41.18:8080/domain/list";
URL url = new URL(addr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setUseCaches(false);
httpCon.setAllowUserInteraction(false);
httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());

See also:


Unrelated to the concrete problem, the password part of your Authorization header value doesn't seem to be properly Base64-encoded. Perhaps it's scrambled because it was examplary, but even if it wasn't I'd fix your Base64 encoding approach.

  • AFAIK, the REST does not mandate GET not having a request body and there maybe legitimate requests with a body. – mirage Nov 7 '16 at 12:48
  • @mirage: That's not specified by REST and would be just ignored. Only badly implemented servers/APIs might require it. This should never be relied upon. – BalusC Nov 7 '16 at 12:49
  • but does it mean that this is prohibited? I mean, HttpURLConnection seems to prohibit the use of GET with bodies, but is this non-standard? There are many libraries which allow it's use. – mirage Nov 7 '16 at 12:53
  • @BalusC Couldin't find any proof in the Java source code that httpCon.setDoOutput(true); sets the method to POST. – Artem Novikov Mar 29 at 11:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.