Wednesday, July 29, 2009

Blackberry POST Request To the Server with Header information

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;
}
Post a Comment