日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 自定义http头_HttpClient自定义HTTP头

發(fā)布時(shí)間:2025/3/15 编程问答 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 自定义http头_HttpClient自定义HTTP头 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

HTTP消息可以包含許多描述消息屬性的標(biāo)頭,例如內(nèi)容長度,內(nèi)容類型,授權(quán)等。 HttpClient提供了檢索,添加,刪除和枚舉標(biāo)頭的方法。 在下面的教程中,我們將演示如何將自定義HTTP頭添加到HttpClient和Http請(qǐng)求方法。

Maven依賴關(guān)系

我們使用maven來管理依賴關(guān)系,并使用Apache HttpClient 4.5版本。 將以下依賴項(xiàng)添加到您的項(xiàng)目中。

pom.xml 文件的內(nèi)容如下 -

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上設(shè)置默認(rèn)標(biāo)頭。 接下來,我們?cè)谙⑸咸砑幼远xHTTP請(qǐng)求標(biāo)頭。

文件:HttpClientRedirectHandlingExample.java -

package com.yiibai.httpdemo;

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 < 300) {

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

}

}

}

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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"

}

}

¥ 我要打賞

糾錯(cuò)/補(bǔ)充

收藏

加QQ群啦,易百教程官方技術(shù)學(xué)習(xí)群

注意:建議每個(gè)人選自己的技術(shù)方向加群,同一個(gè)QQ最多限加 3 個(gè)群。

總結(jié)

以上是生活随笔為你收集整理的java 自定义http头_HttpClient自定义HTTP头的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。