【问题标题】:HttpURLConnection conn.getRequestProperty return nullHttpURLConnection conn.getRequestProperty 返回 null
【发布时间】:2012-07-17 15:48:33
【问题描述】:

我正在尝试将一些数据推送到 BES 的 URL (MDS_CS)

当我在我的代码中设置一些请求标头并提交请求时,提交的请求标头设置为 null

这是我的代码:

        HttpURLConnection conn =(HttpURLConnection)url.openConnection();
        conn.setDoInput(true);//For receiving the confirmation
        conn.setDoOutput(true);//For sending the data
        conn.setRequestMethod("POST");//Post the data to the proxy
        conn.setRequestProperty("X-Rim-Push-ID", pushId);
        conn.setRequestProperty("Content-Type", "text/html");
        conn.setRequestProperty("X-Rim-Push-Title", "-message");
        conn.setRequestProperty("X-Rim-Push-Type", "browser-message");                 
        conn.setRequestProperty("X-Rim-Push-Dest-Port", "7874");            
        //Write the data
        OutputStream out = conn.getOutputStream();
        out.write(data.getBytes());
        out.close();

        System.out.println(conn.getHeaderField("X-Rim-Push-ID"));

当我尝试检索 X-Rim-Push-Title 时,最后一行返回 null,它是 NULL 只有 X-Rim-Push-ID 被正确检索,

请帮帮我

【问题讨论】:

  • 您正在设置请求属性,但获得了一个标头字段。这是故意的吗?我个人希望conn.getRequestProperty("X-Rim-Push-ID") 能够工作。
  • 澄清@Thor84no 的意思,getHeaderField() 用于获取 response 标头,而不是您之前设置的请求标头。
  • 真的,请问,我怎样才能获得完整的 http 帖子以及我发送的标题和数据?来自服务器的响应是 200,但仍然没有将我的数据推送到黑莓
  • @jtahlborn ,@Thor84no ,我怎么能得到我提交的完整请求,就像我说服务器的响应是 200 但我对我的请求进行了回应
  • 设置某种代理来监视 http 流量。我使用charles proxy,效果很好。

标签: java http web httpurlconnection


【解决方案1】:

不太确定你真正想做什么。但是要查看发布到服务器的内容,您必须将其发布到您自己的服务器上并读取您在那里收到的数据。

如果您想查看所有 REQUEST 标头,您可以:

for (String header : conn.getRequestProperties().keySet()) {
   if (header != null) {
     for (String value : conn.getRequestProperties().get(header)) {
        System.out.println(header + ":" + value);
      }
   }
}

或者在连接后你可以打印出响应头:

for (String header : conn.getHeaderFields().keySet()) {
   if (header != null) {
     for (String value : conn.getHeaderFields().get(header)) {
        System.out.println(header + ":" + value);
      }
   }
}

【讨论】:

  • 我正在使用一个简单的主要方法来执行我的请求我无法调试发送的请求!我如何“将其发布到您自己的阅读中”?
  • 要将其发布到您自己的服务器,最简单的方法是在 servlet 容器(如 tomcat)中使用 servlet。但我不明白为什么你真的需要这个。当您在请求中写入所有数据时,您应该知道您发布的内容。你检查过你的data 的样子,编码是否正确?
  • 从 servlet 上的 post 方法处理?我对吗 ?数据被编码!服务器返回一个 200 代码
  • 您是否真的查看了所有标题和可能的响应消息?从响应中读取 InputStream 以查看服务器返回的内容。
【解决方案2】:

我建议使用Apache HttpClient

final HttpClient client = new HttpClient();
final PostMethod method = new PostMethod(uri);
method.addRequestHeader("X-Rim-Push-Title", "-message");
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
Header[] headers = method.getResponseHeaders();

HttpClient 是一种比 HttpURLConnection 更强大的 HTTP 处理方式。

【讨论】:

    【解决方案3】:

    当我检查我提交的标头和请求的输入流时,我得到 200 ok 状态,但没有任何东西发送到设备

    from server: 200 | OK
    
    
    =======REQUEST===============
    request header:X-Rim-Push-ID:pushID:1342694818991
    request header:Host:win-uhgr7vs88uz.assabb.com:8080
    request header:Content-Length:19
    request header:X-Rim-Push-Title:-message
    request header:User-Agent:Java/1.6.0_18
    request header:POST /push?DESTINATION=bestest%40assa-associates.com&PORT=7874&REQUESTURI=/ HTTP/1.1:null
    request header:Content-Type:text/html
    request header:Accept:text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    request header:Connection:keep-alive
    request header:X-Rim-Push-Dest-Port:7874
    request header:X-Rim-Push-Type:browser-message
    ======Response===============
    response header:Date:Thu, 19 Jul 2012 10:47:10 GMT
    response header:Content-Length:0
    response header:X-RIM-Push-ID:pushID:1342694818991
    response header:Via:MDS_5.0.3.26
    response header:x-rim-multidest-push-supported:true
    response header:Server:Apache-Coyote/1.1
    response header:x-rim-push-persisted:fals
    

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多