30

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?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Lesya Makhova
  • 1,190
  • 2
  • 13
  • 27
  • Does the basic authentication travel over the wire via POST? – cdeszaq Jan 06 '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 03:58

1 Answers1

68

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.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • AFAIK, the REST does not mandate GET not having a request body and there maybe legitimate requests with a body. – mirage Nov 07 '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 07 '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 07 '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 '18 at 11:19
  • @ArtemNovikov: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/net/www/protocol/http/HttpURLConnection.java/#1261 – BalusC Mar 29 '18 at 11:47
  • @BalusC thanks! I expected it to happen directly in `setDoOutput`. – Artem Novikov Mar 29 '18 at 11:55