Uploading data in Blackberry using POST method in HTTPS
I am trying to figure out how to upload a data using HTTPS POST method in Blackberry. The following code worked well when i tried it on HTTP. I just want to know what did i miss here
String _postData = "LATITUDE="+lat+"&LONGITUDE="+lng+"&"+"NAME="+name+"&NRIC="+ICNo+"&PLATENO="+VehicleNo+"&CONTACTNO="+ContactNo+"&LOCATION="+ADDRESS;
OutputStream os;
//the preferred network is already arranged here
int[] preferredTransportTypes = {TransportInfo.TRANSPORT_TCP_WIFI, TransportInfo.TRANSPORT_WAP2, TransportInfo.TRANSPORT_TCP_CELLULAR};
ConnectionFactory factory = new ConnectionFactory();
factory.setPreferredTransportTypes(preferredTransportTypes);
ConnectionDescriptor connDescriptor = factory.getConnection("https://example.com");
if(connDescriptor==null){
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
开发者_开发问答 Dialog.alert("Invalid Network. Please check your network set up.");
}
});
}else{
final HttpsConnection https = (HttpsConnection) connDescriptor.getConnection();
https.setRequestMethod(HttpsConnection.POST);
byte [] postDataBytes = _postData.getBytes("UTF8");
`enter code here`https.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
https.setRequestProperty("Content-Language", "en-US");
https.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
os = https.openOutputStream();
os.write(postDataBytes);
os.flush();
os.close();
int state= https.getResponseCode();
if(state==HttpsConnection.HTTP_OK){
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("SOS sent. Our customer care personnel will contact you shortly.");
}
});
} else {
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Failed to send SOS .");
}
});
}
}
} catch(Exception e) {}
When the above code was executed, the application received HttpsConnection.HTTP_OK and show the dialog that the SOS was sent. But the data is not uploaded in the server.
精彩评论