HTTP消息可以包含许多描述消息属性的标头,例如内容长度,内容类型,授权等。 HttpClient提供了检索,添加,删除和枚举标头的方法。 在下面的教程中,我们将演示如何将自定义HTTP头添加到HttpClient和Http请求方法。
Maven依赖关系
我们使用maven来管理依赖关系,并使用Apache HttpClient 4.5版本。 将以下依赖项添加到您的项目中。
pom.xml 文件的内容如下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.yiibai.httpclient.httmethods
http-get
1.0.0-SNAPSHOT
https://memorynotfound.com
httpclient - ${project.artifactId}
org.apache.httpcomponents
httpclient
4.5.2
maven-compiler-plugin
3.5.1
1.8
1.8
自定义HTTP头示例
HttpClient允许我们添加,编辑,删除或枚举http头。 首先来看看在HttpClient上设置默认标头。 接下来,我们在消息上添加自定义HTTP请求标头。
文件:HttpClientRedirectHandlingExample.java –
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* This example demonstrates how to use custom http headers.
*/
public class HttpClientCustomHeadersExample {
public static void main(String... args) throws IOException {
// create custom http headers for httpclient
List defaultHeaders = Arrays.asList(
new BasicHeader("X-Default-Header", "default header httpclient"));
// setting custom http headers on the httpclient
CloseableHttpClient httpclient = HttpClients
.custom()
.setDefaultHeaders(defaultHeaders)
.build();
try {
// setting custom http headers on the http request
HttpUriRequest request = RequestBuilder.get()
.setUri("http://httpbin.org/headers")
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.setHeader(HttpHeaders.FROM, "https://memorynotfound.com")
.setHeader("X-Custom-Header", "custom header http request")
.build();
System.out.println("Executing request " + request.getRequestLine());
// Create a custom response handler
ResponseHandler responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(request, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
} finally {
httpclient.close();
}
}
}
执行上面示例代码,得到以下结果 –
Executing request GET http://httpbin.org/headers HTTP/1.1
----------------------------------------
{
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Type": "application/json",
"From": "https://memorynotfound.com",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.5 (Java/1.8.0_65)",
"X-Custom-Header": "custom header http request",
"X-Default-Header": "default header httpclient"
}
}
今天的文章请求头 content-type_请求头中的Referer分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/59152.html