Http请求POST与GET(Http Requests POST vs GET)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
Http请求POST与GET(Http Requests POST vs 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(); }

如果是这样,为什么? 有什么不同? 我会很感激任何反馈。

谢谢!

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!

最满意答案

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

内容类型必须匹配postData的实际格式。 仅当内容类型实际上是URL编码时,才需要application/x-www-form-urlencoded的内容类型。 例如,您正在编码POST数据,如下所示:

String data = "param1=" + URLEncoder.encode(param1, "UTF-8") + "&param2=" + 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") + "&param2=" + 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));

更多推荐

本文发布于:2023-04-17 08:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/54603f7fbc73051aba5b3874c3eb0594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Http   POST   Requests

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!