public static String ProcessPOSTRequest(String serviceUrl, String authToken, String requestData)
{
HttpConnection hc = null ;
OutputStream os = null;
try {
hc = (HttpConnection) Connector.open(serviceUrl,Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Content-Type", "text/xml");
//hc.setRequestProperty("Content-Language", "en-US");
//hc.setRequestProperty("Accept", "application/octet-stream");
if (authToken.length()>0)
{
byte[] ByteArray=authToken.getBytes("UTF-8");
String Encoded = Base64OutputStream.encodeAsString(ByteArray, 0, ByteArray.length, false, false);
hc.setRequestProperty("Authorization",Encoded);
}
os = hc.openOutputStream();
os.write(requestData.getBytes(),0,requestData.getBytes().length);
os.flush();
os.close();
int responseCode = hc.getResponseCode();
System.out.println(responseCode);
System.out.println(hc.getResponseMessage());
InputStream inputStream = hc.openInputStream();
StringBuffer sb = new StringBuffer();
int C;
while( -1 != (C = inputStream.read()))
{
sb.append((char)C);
}
return sb.toString();
} catch (Exception e) {
Dialog.alert(e.getMessage());
}
return null;
}
Comments
Never forget to put Content-Length in your header while making a POST request. I've been into troubles many time when neglected this. It caused issue like my HTTP POST requests from Blackberry never reaches to server, the fix was to put correct Content-Length header with correct length of passing data in bytes.
example:
hc.setRequestProperty("Content-Length", 583);
I hope it would help.
Thanks,
Sameer.
Can anyone please explain what are the above two parameters for?
My Scenario:
I want to call a webservice for login. I have the URL www.abc[dot]com[slash]webservice.cfm?mode=Login&userid=myId@hotmail.com&password=1234567
How do the above mentioned parameters fit in my scenario ..
thanks
if you are using this kind of thing
www.abc[dot]com[slash]webservice.cfm?mode=Login&userid=myId@hotmail.com&password=12345
It is a get request not a post request. get request can retrieve data by directly calling the url by attaching the params as u mentioned.