问题描述
我正在尝试使用应用引擎应用程序下的urlfetch执行POST请求。
I am trying to do a POST request using urlfetch under an app engine application.
我已按照从以下简单示例中提取的说明(和代码)进行操作App Engine文档(此处),在使用HttpURLConnection部分下。
I have followed the instructions (and code) extracted from the simple example found at the App Engine documentation (here https://developers.google.com/appengine/docs/java/urlfetch/usingjavanet), under the section "Using HttpURLConnection".
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
String message = URLEncoder.encode("my message", "UTF-8");
try {
URL url = new URL("http://httpbin.org/post");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("message=" + message);
writer.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// OK
} else {
// Server returned HTTP error code.
}
} catch (MalformedURLException e) {
// ...
} catch (IOException e) {
// ...
}
为了测试此POST请求,我使用以下网站。
In order to test this POST request, I am using the following website "http://httpbin.org/post".
获取和连接工作原理 - 但是,连接是作为GET而不是POST发送的。
The fetch and connection works - however, the connection is sent as a GET and not as POST.
以下是我对此请求的回复:
Here is the response I get from for this request:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1><p>The method GET is not allowed for the requested URL.</p>
是否有人遇到此问题?
Have anybody run into this issue ?
感谢任何帮助。
推荐答案
因为这个问题仍然存在3年后相当多的观点,我将在此确认适用于对给定URL发出POST请求,并且自该问题首次发布以来一直保持不变。工作代码示例如下:
As this question still gets a considerable amount of views 3 years later, I'll confirm here that the method given in the documentation sample works fine for making a POST request to the given URL, and has remained unchanged since this question was first posted. The working code sample is as follows:
String message = URLEncoder.encode("my message", "UTF-8");
URL url = new URL("http://httpbin.org/post");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("message=" + message);
writer.close();
StringBuffer responseString = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
responseString.append(line);
}
reader.close();
收到以下回复:
Code: 200, message: { "args": {}, "data": "", "files": {}, "form": {
"message": "my message" }, "headers": { "Accept-Encoding":
"gzip,deflate,br", "Content-Length": "18", "Content-Type":
"application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent":
"AppEngine-Google; (+http://code.google.com/appengine; appid: s~
<redacted>)", "X-Cloud-Trace-Context": ",<redacted>" }, "json": null,
"origin": "107.178.194.113", "url": "http://httpbin.org/post"}
这篇关于用于POST请求的HttpURLConnection App Engine Java示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!