Java @ Desk: Web Service | Java at your desk

Add, get, set custom HttpHeader in request of HttpClient

Add, get, set custom HttpHeader in request of HttpClient

There are two ways to call the webservice using HTTP client one with Get method and other with Post method.

To add a header in HttpGet or HttpPost method, we need to use the addHeader() method which holds the key and value. Key in this has to be unique just like Map. You can add multiple headers.

How to call Rest CXF web service using HttpClient?

There are two ways to call the webservice using HTTP client one with Get method and other with Post method.

HttpClient call to GET webservice: Call a web service includes following steps:

1) Create the HttpGet object by passing the URL of the webservice
2) Append the request parameters if any in the URL

HttpGet get = new HttpGet("http://localhost:8080" + "/APP_NAME/JAXRS_ADDRESS/PATH" + "?" + "REQUEST_PARAM=REQUEST_VALUE");
3) Add the headers if any as shown below

get.addHeader("userId", );
get.addHeader("Service", );