0

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

4

1 回答 1

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();
   }
于 2012-12-12T02:51:11.603 回答