我在编写一个使用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和GET操作。然而,我担心我没有正确地进行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();
}
如果是这样,为什么呢?有什么区别?我会非常感激任何反馈。
谢谢!