BlackBerry通过HttpConnection来发送数据到Web服务器

 BlackBerry手机向Web服务器提交数据,可以通过HttpConnection的方式发送,写了一个方法,直接调用即可,代码如下:

     public void postAgent() throws IOException{
          InputStream istrm = null;
          HttpConnection hconn = null;
          OutputStream out = null;
           try
               {
               String bodystr = "发送的内容";
               
               String URL = http://127.0.0.1/mycode;
               hconn = (HttpConnection)Connector.open(URL);
               hconn.setRequestMethod(HttpConnection.POST);
               hconn.setRequestProperty("User-Agent", "BlackBerry/4.2.0");
               hconn.setRequestProperty("Content-Length",String.valueOf(bodystr.getBytes().length));
               hconn.setRequestProperty("Content-Type","text/xml;charset=gb2312");
               out = hconn.openOutputStream();
               out.write(bodystr.getBytes());
              
               if(hconn.getResponseCode()==HttpConnection.HTTP_OK)
               {
                   istrm = hconn.openInputStream();
                   int length = (int) hconn.getLength();
                   if(length>0)
                    {
                        byte servletData[] = new byte[length];
                        istrm.read(servletData);
                        }
                     else
                       Dialog.alert("错误");
                   }
               Dialog.alert("发送完毕");

                 }
               catch(Exception e)
                 {System.out.println(e.toString());}
               finally
               {
                   if(istrm!=null)
                      istrm.close();
                   if(out!=null)
                      out.close();
                   if(hconn!=null)
                      hconn.close();
                   }
           }  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值