使用HTTPS中的POST方法在Blackberry上传数据

时间:2011-07-18 09:04:14

标签: java blackberry post https

我试图弄清楚如何在Blackberry中使用HTTPS POST方法上传数据。当我在HTTP上尝试它时,以下代码运行良好。我只是想知道我在这里想念的是什么

            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) {}

执行上述代码后,应用程序收到HttpsConnection.HTTP_OK并显示SOS发送的对话框。但数据未上传到服务器中。

0 个答案:

没有答案