显示HTTP请求信息 - BlackBerry

时间:2012-12-11 14:50:33

标签: blackberry request httpconnection

如何获取所有HTTP请求标头,方法,连接后缀以及我添加到请求中的所有参数?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情(我在后台线程上运行此代码,我为什么使用UiApplication.invokeLater()来显示结果):

   try {
      ConnectionFactory factory = new ConnectionFactory();  // for OS 5.0+
      factory.setPreferredTransportTypes(new int[] { 
            TransportInfo.TRANSPORT_TCP_WIFI, 
            TransportInfo.TRANSPORT_TCP_CELLULAR 
      });
      // For OS < 5.0
      //HttpConnection conn = (HttpConnection) Connector.open("http://www.google.com;interface=wifi");
      HttpConnection conn = (HttpConnection) factory.getConnection("http://www.google.com").getConnection();
      conn.setRequestProperty("sessionId", "ABCDEF0123456789");

      final StringBuffer results = new StringBuffer();

      String key = "";
      int index = 0;
      // loop over all the header fields, and record their values
      while (key != null) {
         key = conn.getHeaderFieldKey(index);
         if (key != null) {
            String value = conn.getHeaderField(key);
            results.append(key + " = " + value + "\n\n");
         }
         index++;
      }

      results.append("method = " + conn.getRequestMethod() + "\n\n");

      // we (should) know which request properties we've set, so we ask 
      //  for them by name here
      String sessionId = conn.getRequestProperty("sessionId");
      results.append("sessionId = " + sessionId + "\n\n");

      String url = conn.getURL();
      results.append("URL = " + url);

      // show the result on screen (UI thread)
      UiApplication.getUiApplication().invokeLater(new Runnable() {
         public void run() {
            textField.setText(results.toString());                                                
         }
      });
   } catch (IOException e) {
      e.printStackTrace();
   }