如何使用Google App Engine的Java運行時的UrlFetch服務執行HTTP PUT請求?Java GAE UrlFetch是否支持HTTP PUT?
在我的應用程序下面的代碼將發送POST請求:
URL url = ...;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(MAX_TIMEOUT); // Maximum allowable timeout on GAE of 60 seconds
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(conn.getOutputStream(), Charset.forName("utf-8"));
writer.write(jsonContent);
} finally {
if (writer != null)
writer.close();
}
}
InputStream inStream = null;
E response = null; // Unmarshalled using Jackson
try {
inStream = conn.getInputStream();
if (status != 204) { // NO CONTENT => No need to use Jackson to deserialize!
response = mapper.readValue(inStream, typeRef);
}
} finally {
if (inStream != null) {
inStream.close();
}
}
我嘗試做相同的POST
爲PUT
,但我一直得到一個 HTTP錯誤不允許的方法。謝謝您的幫助。
參考:https://developers.google.com/appengine/docs/java/urlfetch/overview
不幸的是,GAE Java沒有良好的文檔作爲Python的版本GAE URL的獲取(https://developers.google.com/appengine/docs/python/urlfetch/overview)。任何來自Google的人都想說明原因?
**注:**'typeRef'是TypeReference例如,'mapper'是ObjectMapper實例 – ecbrodie