开发者

How do we pass parameters when doing HTTP Post?

I am working on an app where we have to pass specific web api parameters to a web app using HTTP POST. eg: apimethod name parameter1 value paramet开发者_运维知识库er2 value So do I use a string or URLEncodedPostData to send that data? It would be good if u help me with a code eg. I am using something like this but it doesnt post the data to the server. Though the response code is ok/200 and I also get get a parsed html response when i read the httpresponse input stream. But the code doesnt post anything. So unable to get the expected response.

             _postData.append("method", "session.getToken");
             _postData.append(  "developerKey", "value");
             _postData.append(  "clientID", "value");

            _httpConnection = (HttpConnection) Connector.open(URL, Connector.READ_WRITE);
            String encodedData = _postData.toString();
            _httpConnection.setRequestMethod(HttpConnection.POST);
            _httpConnection.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
            _httpConnection.setRequestProperty("Content-Language", "en-US");
            _httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            _httpConnection.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString());
            os = _httpConnection.openOutputStream();
            os.write(requeststring.getBytes());`


The code you posted above looks correct - although you'll want to do a few more things (maybe you did this already but didn't include it in your code):

  1. Close the outputstream once you've written all the bytes to it
  2. Call getResponseCode() on the connection so that it actually sends the request


POSTed parameters are usually sent in the response BODY, which means URL-encoding them is inappropriate. Quote from the HTTP/1.1 protocol:

  Note: The "multipart/form-data" type has been specifically defined
  for carrying form data suitable for processing via the POST
  request method, as described in RFC 1867 [15].


The post method allows you to use pretty arbitrary message bodies — so it is whatever format the server wants.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜