Http 请求 POST 与 GET [英] Http Requests POST vs GET

查看:39
本文介绍了Http 请求 POST 与 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写的使用 OAuth 的应用程序中使用了大量 HTTP 请求.目前,我以相同的方式发送 GET 和 POST 请求:

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

这工作正常.我成功发布和获取.但是,我担心我没有以正确的方式进行 POST.我需要在上面的代码中包含以下 if 语句吗?

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.

谢谢!

推荐答案

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

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

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.

还有,

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.

也就是说,如果您知道请求方法将自动"设置为 POST 的事实,那么转换到 HttpUrlConnection 是不必要的:

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

这篇关于Http 请求 POST 与 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆