Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: setRequestMethod not works

I have the next part of code:

dCon = (HttpURLConnection) new URL(torrentFileDownloadLink).openConnection();
dCon.setRequestProperty("Cookie", "uid=" + cookies.get("uid") + ";pass=" + cookies.get("pass"));
dCon.setRequestMethod("GET");
dCon.setConnectTimeout(30000);
dCon.setDoOutput(true);

But Wireshark shows that request method is "POST". What am I doing wrong or this is just a bug? Btw, getRequestMethod say that method is "GET" but in reality it's POST.

like image 464
Clark Avatar asked Dec 16 '22 15:12

Clark


1 Answers

Setting the URLConnection#setDoOutput() to true means that you're about to write request data to the request body by URLConnection#getOutputStream(). This is impossible in combination with GET (which expects the request parameters in the request URL), thus the request method will implicitly be set to POST.

If you don't need to write any data to the request body, then just remove that line. It defaults to false (and thus GET) anyway.

See also:

  • How to use URLConnection to fire and handle HTTP requests?
like image 195
BalusC Avatar answered Dec 26 '22 12:12

BalusC