Http 请求 POST 与 GET

发布于 2024-08-26 21:54:05 字数 1091 浏览 6 评论 0 原文

我在编写的使用 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();
                }

如果是这样,为什么?有什么区别?如果有任何反馈,我将不胜感激。

谢谢!

I am using a lot of HTTP Requests in an application that I am writing which uses OAuth. Currently, I am sending my GET and POST requests the same way:

HttpConnection connection = (HttpConnection) Connector.open(url
                    + connectionParameters);

            connection.setRequestMethod(method);
            connection.setRequestProperty("WWW-Authenticate",
                    "OAuth realm=api.netflix.com");

            int responseCode = connection.getResponseCode();

And this is working fine. I am successfully POSTing and GETing. However, I am worried that I am not doing POST the right way. Do I need to include in the above code the following if-statement?

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();
                }

If so, why? What's the difference? I would appreciate any feedback.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猛虎独行 2024-09-02 21:54:05
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

内容类型必须与 postData实际格式匹配。仅当内容类型实际上是 application/x-www-form-urlencoded 内容类型rel="nofollow noreferrer">url 编码。例如,您按如下方式对 POST 数据进行编码:

String data = "param1=" + URLEncoder.encode(param1, "UTF-8")
           + "¶m2=" + URLEncoder.encode(param2, "UTF-8");

这样另一方将能够根据指定的格式解析数据而不会破坏它。

并且,

connection.setRequestProperty("Content-Length", Integer.toString(postData.length));

这对于确保稳健的数据传输是优选的。如果您忽略此操作并且连接以某种方式断开,那么另一方将永远无法确定内容是否完全流入。

也就是说,如果您知道请求方法将“自动”设置为 POST(如果您这样做),则无需强制转换为 HttpUrlConnection

connection.setDoOutput(true);

或者在您的情况下更合适:

connection.setDoOutput("POST".equals(method));
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

The content type must match the actual format of the postData. A content type of application/x-www-form-urlencoded is only necessary if the content type is actually url encoded. E.g. you're encoding POST data as follows:

String data = "param1=" + URLEncoder.encode(param1, "UTF-8")
           + "¶m2=" + URLEncoder.encode(param2, "UTF-8");

This way the other side will be able to parse the data according the specified format without breaking it.

And,

connection.setRequestProperty("Content-Length", Integer.toString(postData.length));

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 to POST if you do:

connection.setDoOutput(true);

or in your case more suitable:

connection.setDoOutput("POST".equals(method));
眼睛会笑 2024-09-02 21:54:05

来自 HTML 规范文档:

如果表单的处理是
幂等(即它没有持久性
对状态的可观察到的影响
world),那么表单方法应该是
得到。很多数据库检索都没有
明显的副作用并使其理想
查询表格的应用。
- -

如果与表单处理相关的服务有副作用

(例如,修改
数据库或订阅
service),方法应该是POST。

它们非常相似,只是目的是主要区别。

From the HTML specification document:

If the processing of a form is
idempotent (i.e. it has no lasting
observable effect on the state of the
world), then the form method should be
GET. Many database searches have no
visible side-effects and make ideal
applications of query forms.
- -

If the service associated with the processing of a form has side effects

(for example, modification of a
database or subscription to a
service), the method should be POST.

They are much the same just the purpose is the major difference.

后eg是否自 2024-09-02 21:54:05

如果寻址的 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.

○闲身 2024-09-02 21:54:05

对修改某些内容的请求使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文