Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add,set and get Header in request of HttpClient?

In my application I need to set the header in the request and I need to print the header value in the console... So please give an example to do this the HttpClient or edit this in my code...

My Code is ,

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List;  import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair;  public class SimpleHttpPut {    public static void main(String[] args) {     HttpClient client = new DefaultHttpClient();     HttpPost post = new HttpPost("http://http://localhost:8089/CustomerChatSwing/JoinAction");     try {       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);       nameValuePairs.add(new BasicNameValuePair("userId",       "123456789"));       post.setEntity(new UrlEncodedFormEntity(nameValuePairs));        HttpResponse response = client.execute(post);       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));       String line = "";       while ((line = rd.readLine()) != null) {     System.out.println(line);       }      } catch (IOException e) {       e.printStackTrace();     }   } }   

Thanks in advance...

like image 738
Human Being Avatar asked Dec 06 '12 12:12

Human Being


People also ask

How do I add a header to my HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.

How would you add a header to a request made with Apache's HttpClient library?

In versions pre 4.3 of HttpClient, we can set any custom header on a request with a simple setHeader call on the request: HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(SAMPLE_URL); request. setHeader(HttpHeaders. CONTENT_TYPE, "application/json"); client.

How do I create a custom header in request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How do I pass HTTP header in SOAP request?

Add(new InspectorBehavior()); that's it, now each SOAP call will be equipped with custom HTTP header "HEADER_WHICH_WE_WANT" with value "Actual Value we want" we specified in our code.


1 Answers

You can use HttpPost, there are methods to add Header to the Request.

DefaultHttpClient httpclient = new DefaultHttpClient(); String url = "http://localhost"; HttpPost httpPost = new HttpPost(url);  httpPost.addHeader("header-name" , "header-value");  HttpResponse response = httpclient.execute(httpPost); 
like image 78
Kang Li Avatar answered Sep 23 '22 17:09

Kang Li