【问题标题】:How to add,set and get Header in request of HttpClient?如何在 HttpClient 的请求中添加、设置和获取 Header?
【发布时间】:2012-11-24 10:59:13
【问题描述】:

在我的应用程序中,我需要在请求中设置标头,并且需要在控制台中打印标头值... 所以请举一个例子来做这个 HttpClient 或在我的代码中编辑这个......

我的代码是,

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();
    }
  }
}  

提前谢谢...

【问题讨论】:

标签: java http servlets httpclient apache-httpclient-4.x


【解决方案1】:

可以使用HttpPost,有一些方法可以在Request中添加Header。

DefaultHttpClient httpclient = new DefaultHttpClient();
String url = "http://localhost";
HttpPost httpPost = new HttpPost(url);

httpPost.addHeader("header-name" , "header-value");

HttpResponse response = httpclient.execute(httpPost);

【讨论】:

  • DefaultHttpClient 已弃用
  • CloseableHttpClient 正在使用中。
【解决方案2】:

在 apache 页面上:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

你有这样的东西:

URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google.com").setPath("/search")
    .setParameter("q", "httpclient")
    .setParameter("btnG", "Google Search")
    .setParameter("aq", "f")
    .setParameter("oq", "");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());

【讨论】:

    【解决方案3】:

    您可以完全按照使用公共 GitHub API 的方式测试此代码(不要超出请求限制):

    public class App {
    
        public static void main(String[] args) throws IOException {
    
            CloseableHttpClient client = HttpClients.custom().build();
    
            // (1) Use the new Builder API (from v4.3)
            HttpUriRequest request = RequestBuilder.get()
                    .setUri("https://api.github.com")
                    // (2) Use the included enum
                    .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
                    // (3) Or your own
                    .setHeader("Your own very special header", "value")
                    .build();
    
            CloseableHttpResponse response = client.execute(request);
    
            // (4) How to read all headers with Java8
            List<Header> httpHeaders = Arrays.asList(response.getAllHeaders());
            httpHeaders.stream().forEach(System.out::println);
    
            // close client and response
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多