我想将请求方法从 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);
但正如您在调试请求的方法时在此图中看到的那样,请求的方法没有改变:
那只是一个实施细节 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/net/www/protocol/https/HttpsURLConnectionImpl.java#HttpsURLConnectionImpl.setRequestMethod%28java.lang.String%29. The HttpsURLConnectionImpl
对象具有对委托的引用,并设置该委托的请求方法。如果您扩展delegate
字段并检查其method
场,你会看到POST
.
如果您继续执行该代码,您还会看到它尝试发送 POST 请求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)