0

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 . I want to set parameters inside header which I can retrieve on my web server. 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 . I tried to do it with addRequestProperty and setRequestProperty both, but still the values are not getting set inside my header.

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.

AppleBud
  • 1,501
  • 8
  • 31
  • 47

1 Answers1

0

Have you tried Apache http client? 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);
MjZac
  • 3,476
  • 1
  • 17
  • 28
  • Will I be able to fetch this header value on my server code ? Because right now, When i try to fetch the header , it won't show the value there. – AppleBud Aug 26 '14 at 06:47
  • yes, you will be able to fetch the header at the server side. And since you are using `php 5.3.3` you might want to have a look at [this](http://www.omaroid.com/php-get-and-set-custom-http-headers/) post too. – MjZac Aug 26 '14 at 07:03