[英]Unable to set property in header using HTTPUrlConnection in GET request
I tried to found a solution for this, even found numerous solutions but none resolved my issue. 我试图为此找到一个解决方案,甚至找到了很多解决方案,但都没有解决我的问题。 I am trying to implement a Java client which will consume a
Restful web service
. 我正在尝试实现一个Java客户端,该客户端将使用
Restful web service
。 I want to set parameters inside header which I can retrieve on my web server. 我想在可以在Web服务器上检索的标头中设置参数。 My code is;
我的代码是;
URL url = new URL("http://servierIP/address");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("firstParam", "hi");
conn.addRequestProperty("secondParam","12345" );
conn.setRequestProperty("Content-Type", "application/xml");
conn.connect();
However, when I try to get values from header, I get NULL
. 但是,当我尝试从标头中获取值时,我得到了
NULL
。 I tried to do it with addRequestProperty
and setRequestProperty
both, but still the values are not getting set inside my header. 我尝试同时使用
addRequestProperty
和setRequestProperty
来实现,但是仍然没有在标头中设置值。
and the output I am getting when I try to fetch header value is: 当我尝试获取标头值时得到的输出是:
null : [HTTP/1.1 200 OK]
Expires : [Thu, 19 Nov 1981 08:52:00 GMT]
Access-Control-Allow-Orgin : [*]
Set-Cookie : [dsfjskfjdsfjskf ---- some value]
Access-Control-Allow-Methods : [*]
Connection : [Keep-Alive]
Server : [Apache/2.2.15 (CentOS)]
X-Powered-By : [PHP/5.3.3]
Cache-Control : [no-store, no-cache, must-revalidate, post-check=0, pre-check=0, public, public]
Pragma : [no-cache, public, public]
Date : [Tue, 26 Aug 2014 05:33:10 GMT]
Transfer-Encoding : [chunked]
Content-Type : [application/xml; charset=utf-8]
If anyone can resolve the issue, I would be very grateful.. 如果任何人都可以解决问题,我将不胜感激。
Thank you. 谢谢。
Have you tried Apache http client ? 您是否尝试过Apache http客户端 ? Adding headers is quite simple
添加标题非常简单
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urlString);
httpget.addHeader("my-custom-header", "value");
HttpResponse response = httpclient.execute(httpget);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.