Http 请求 POST 与 GET
我在编写的使用 OAuth 的应用程序中使用了大量 HTTP 请求。目前,我以相同的方式发送 GET 和 POST 请求:
HttpConnection connection = (HttpConnection) Connector.open(url
+ connectionParameters);
connection.setRequestMethod(method);
connection.setRequestProperty("WWW-Authenticate",
"OAuth realm=api.netflix.com");
int responseCode = connection.getResponseCode();
这工作正常。我已成功发布和获取。但是,我担心我没有以正确的方式进行 POST。我需要在上面的代码中包含以下 if 语句吗?
if (method.equals("POST") && postData != null) {
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer
.toString(postData.length));
OutputStream requestOutput = connection.openOutputStream();
requestOutput.write(postData);
requestOutput.close();
}
如果是这样,为什么?有什么区别?如果有任何反馈,我将不胜感激。
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内容类型必须与
postData
的实际格式匹配。仅当内容类型实际上是 application/x-www-form-urlencoded 内容类型rel="nofollow noreferrer">url 编码。例如,您按如下方式对 POST 数据进行编码:这样另一方将能够根据指定的格式解析数据而不会破坏它。
并且,
这对于确保稳健的数据传输是优选的。如果您忽略此操作并且连接以某种方式断开,那么另一方将永远无法确定内容是否完全流入。
也就是说,如果您知道请求方法将“自动”设置为
POST
(如果您这样做),则无需强制转换为HttpUrlConnection
:或者在您的情况下更合适:
The content type must match the actual format of the
postData
. A content type ofapplication/x-www-form-urlencoded
is only necessary if the content type is actually url encoded. E.g. you're encoding POST data as follows:This way the other side will be able to parse the data according the specified format without breaking it.
And,
This is preferable to ensure a robust data transfer. If you omit this and the connection somehow get broken, then the other side will never be able to determine if the content is fully streamed in or not.
That said, the cast to
HttpUrlConnection
is unnecessary if you know the fact that the request method will "automatically" be set toPOST
if you do:or in your case more suitable:
来自 HTML 规范文档:
它们非常相似,只是目的是主要区别。
From the HTML specification document:
They are much the same just the purpose is the major difference.
如果寻址的 HTTP 服务器需要,您需要设置内容类型标头。我们确实没有办法知道它是否存在。
如果您没有显式设置内容长度标头,则应自动计算和设置它,但由于您事先知道它,所以我会设置它,以便在实际发送数据之前,您的内容不会被 HttpConnection 不必要地缓冲。
You need to set the content type header if it's required by the addressed HTTP server. There's really no way for us to know if it is or not.
The content length header should be calculated and set automatically if you don't set it explicitly, but since you know it beforehand anyway, I would set it so that your content is not unnecessarily buffered by the HttpConnection before actually sending the data.
对修改某些内容的请求使用 POST,对搜索或仅获取文档的请求使用 GET。浏览器端的差异在于,浏览器可以避免意外地再次执行 POST 请求,例如通过提示用户进行确认。
处理 POST 请求时,切勿使用文档进行响应,而是将用户重定向到包含“已提交表单”或您想要给出的任何答案的 GET 请求。这可以避免浏览器后退/前进按钮出现问题,因为否则浏览到响应页面将需要重新提交 POST 请求。
Use POST for requests that modify something, GET for requests that do searches or simply get documents. The difference on the browser side is that browsers avoid accidentally doing a POST request again e.g. by prompting the user for confirmation.
When processing a POST request, never respond with a document, but instead redirect the user to a GET request that contains the "form submitted" or whatever answer you want to give. This avoids problems with browser back/forward buttons because otherwise browsing to the response page would require resubmitting the POST request.