我在使用OAuth编写的应用程序中使用了很多HTTP请求。目前,我以同样的方式发送我的GET和POST请求:Http请求POST vs GET
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();
}
如果是这样,为什么?有什么不同?我会很感激任何反馈。
谢谢!
谢谢,BalusC,这是一个非常有用的解释。 – littleK 2010-03-26 19:08:53