如何在 HttpClient 的请求中添加、设置和获取 Header?
Posted
技术标签:
【中文标题】如何在 HttpClient 的请求中添加、设置和获取 Header?【英文标题】:How to add,set and get Header in request of HttpClient? 【发布时间】: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();
提前谢谢...
【问题讨论】:
如果您想为所有请求设置特定的标头,那么您可以在客户端上设置它们,而不是请求:***.com/questions/18707676/… 【参考方案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
【讨论】:
以上是关于如何在 HttpClient 的请求中添加、设置和获取 Header?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用来自 IHttpClientFactory 的 HttpClient 将 cookie 添加到请求中