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

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

问题描述

我在使用OAuth编写的应用程序中使用了很多HTTP请求。目前,我以同样的方式发送我的GET和POST请求:

  HttpConnection连接=(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)& amp ;& 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();
}

如果是这样,为什么?有什么不同?



谢谢!

解决方案

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

内容类型必须与<$ c $的实际 C> POSTDATA 。内容类型 application / x-www-form-urlencoded 仅当内容类型实际上编码的网址。例如。您将对POST数据进行如下编码:

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

通过这种方式,另一方将能够按照指定的格式解析数据而不会破坏数据。 / p>

并且,

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

这对于确保强大的数据传输更为重要。如果你忽略了这一点,并且连接以某种方式被破坏,那么另一方将永远无法确定内容是否完全流入。

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

  connection.setDoOutput(true); 

或者您的情况更适合:

  connection.setDoOutput( POST .equals(方法)); 


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

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

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

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