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...
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With