加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – HttpURLConnection conn.getRequestProperty返回null

发布时间:2020-12-15 00:25:57 所属栏目:Java 来源:网络整理
导读:我正在尝试将一些数据推送到BES的URL(MDS_CS) 当我在我的代码中设置一些请求标头并提交请求时,提交请求的标头设置为null. 这是我的代码: HttpURLConnection conn =(HttpURLConnection)url.openConnection(); conn.setDoInput(true);//For receiving the con
我正在尝试将一些数据推送到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,

请帮我

解决方法

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

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

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

或者在连接后,您可以打印出RESPONSE标题:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读