Java:setRequestMethod不起作用

Cla*_*ark 3 java methods url http request

我有下一部分代码:

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);
Run Code Online (Sandbox Code Playgroud)

但Wireshark显示请求方法是"POST".我做错了什么或者这只是一个错误?顺便说一句,getRequestMethod说方法是"GET",但实际上它是POST.

Bal*_*usC 11

设置URLConnection#setDoOutput()true表示您要将请求数据写入请求正文URLConnection#getOutputStream().这与GET(需要请求URL中的请求参数)结合使用是不可能的,因此请求方法将隐式设置为POST.

如果您不需要将任何数据写入请求正文,则只需删除该行.false无论如何它默认为(因此GET).

也可以看看: