java HttpURLConnection conn.getRequestProperty 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11544256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpURLConnection conn.getRequestProperty return null
提问by dzgeek
I'm trying to push some data to an URL (MDS_CS) for a BES
我正在尝试将一些数据推送到 BES 的 URL (MDS_CS)
when i set some Request Headers in my code, and submit the request, the submited request's header is set to null.
当我在代码中设置一些请求标头并提交请求时,提交的请求标头设置为null。
here is my code :
这是我的代码:
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"));
the last line return null when i try to retrieve the X-Rim-Push-Titleit is NULLonly X-Rim-Push-IDwhich is correctly retrieved,
当我尝试检索X-Rim-Push-Title时,最后一行返回 null它 仅是NULL正确检索的X-Rim-Push-ID,
please help me
请帮我
回答by morja
Not quite sure what you really want to do. But to see what is posted to the server you would have to post it to your own and read the data you receive there.
不太确定你真正想要做什么。但是要查看发布到服务器的内容,您必须将其发布到您自己的服务器并读取您在那里收到的数据。
If you want to see all the REQUEST headers you could:
如果您想查看所有 REQUEST 标头,您可以:
for (String header : conn.getRequestProperties().keySet()) {
if (header != null) {
for (String value : conn.getRequestProperties().get(header)) {
System.out.println(header + ":" + value);
}
}
}
Or after connecting you can print out the RESPONSE headers:
或者在连接后,您可以打印出 RESPONSE 标题:
for (String header : conn.getHeaderFields().keySet()) {
if (header != null) {
for (String value : conn.getHeaderFields().get(header)) {
System.out.println(header + ":" + value);
}
}
}
回答by Kieran
I would suggest using Apache HttpClient
我建议使用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 is a much more powerful way of dealing with HTTP than HttpURLConnection.
HttpClient 是一种比 HttpURLConnection 更强大的 HTTP 处理方式。
回答by dzgeek
When I check my headers submitted and the inputstream of request, I get the 200 ok status, but nothing is sent to device
当我检查提交的标头和请求的输入流时,我得到 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