如何使用针对Google App Engine的Java运行时的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
相同的操作,但我继续为“不允许的方法”获取 405 HTTP错误。谢谢你的帮助。
参考: https://developers.google.com/appengine/docs/java/urlfetch/overview
不幸的是,GAE Java没有Python的GAE URL提取版本(https://developers.google.com/appengine/docs/python/urlfetch/overview)那么好的文档。谷歌的任何人都想说为什么?
答案 0 :(得分:1)
我认为您的代码有效。您看到的HTTP 405
来自您正在联系的服务器(并非所有服务支持PUT
)。您可以尝试使用curl
点击该网址,如果您想要确认您的代码不是您的错误:
curl -X PUT -d "foo=bar" http://your_site.com/path/to/your/resource