java HttpURLConnection.setRequestMethod()不起作用

Ste*_*Ste 10 java httprequest

我想将请求方法从GET更改为POST.这是我的代码:

HttpURLConnection connection = null;  

     URL url = new URL("https://accounts.google.com/o/oauth2/token");

     connection = (HttpURLConnection) url.openConnection();
     connection.setRequestMethod("POST");
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
     connection.setUseCaches (false);
     connection.setDoInput(true);
     connection.setDoOutput(true);
Run Code Online (Sandbox Code Playgroud)

但正如您在调试请求的方法时在此图像中看到的那样不会改变:

在此输入图像描述

Sot*_*lis 20

这只是一个实现细节.该HttpsURLConnectionImpl对象具有对委托的引用,并设置该委托的请求方法.如果您展开delegate字段并检查其method字段,您将看到POST.

如果您按照代码进行操作,您还会看到它尝试发送POST请求.