2
我試圖使用HttpsUrlConnection將HTTP POST請求發送到URL。這是我想要做的:無法將HttpURLConnection方法切換到POST
URL requestedUrl = null;
HttpURLConnection urlConnection = null;
try {
requestedUrl = new URL("https://www.test.local/login/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
urlConnection = (HttpsURLConnection) requestedUrl.openConnection();
try {
urlConnection.setRequestMethod("POST");
} catch (ProtocolException e1) {
e1.printStackTrace();
}
urlConnection.setRequestProperty("Content-Type","application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(1500);
try {
urlConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
當調試我看到RequestMethod仍然是GET,任何想法爲什麼?
在此先感謝。
'setDoOutput()'已經設置爲POST方法。你做了兩次。 – EJP