【问题标题】:Display HTTP Request Information - BlackBerry显示 HTTP 请求信息 - BlackBerry
【发布时间】:2012-12-11 14:50:33
【问题描述】:

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

【问题讨论】:

    标签: blackberry request httpconnection


    【解决方案1】:

    尝试这样的事情(我在后台线程上运行了这段代码,这就是我使用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();
       }
    

    【讨论】:

    • 我应该添加标签字段还是什么?如何显示我发送的请求?
    • @fou,在我的示例中,textField 只是TextField 的一个实例。我只是使用了可以显示results 字符串的任何东西,其中列出了您要求的信息。
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2016-06-06
    • 1970-01-01
    • 2012-09-11
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多