欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java.net.http.HttpClient使用示例解析

 更新時(shí)間:2023年08月31日 09:53:40   作者:supermassive  
這篇文章主要為大家介紹了java.net.http.HttpClient使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

java自帶HttpClient使用

在使用java自帶的HttpClient時(shí), 發(fā)現(xiàn)一個(gè)現(xiàn)象,即有些以前常用http的header, 是不能夠set到請(qǐng)求里進(jìn)行傳遞的,在jdk.internal.net.http.common.Utils類中,可以看到

private static Set<String> getDisallowedHeaders() {
        Set<String> headers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        headers.addAll(Set.of("connection", "content-length", "expect", "host", "upgrade"));
        String v = getNetProperty("jdk.httpclient.allowRestrictedHeaders");
        if (v != null) {
            // any headers found are removed from set.
            String[] tokens = v.trim().split(",");
            for (String token : tokens) {
                headers.remove(token);
            }
            return Collections.unmodifiableSet(headers);
        } else {
            return Collections.unmodifiableSet(headers);
        }
    }

connection, content-length, expect, host, upgrade這幾個(gè), 屬于禁止設(shè)置的。

校驗(yàn)

因?yàn)樵趈dk.internal.net.http.HttpRequestBuilderImpl中, 會(huì)進(jìn)行嚴(yán)格的校驗(yàn)

private void checkNameAndValue(String name, String value) {
        requireNonNull(name, "name");
        requireNonNull(value, "value");
        if (!isValidName(name)) {
            throw newIAE("invalid header name: \"%s\"", name);
        }
        if (!Utils.ALLOWED_HEADERS.test(name, null)) {
            throw newIAE("restricted header name: \"%s\"", name);
        }
        if (!isValidValue(value)) {
            throw newIAE("invalid header value: \"%s\"", value);
        }
    }

控制連接復(fù)用

它自己控制連接的復(fù)用,所以設(shè)置不了connection這個(gè)header.從jdk.internal.net.http.ConnectionPool中可以看出一些參數(shù)

static final long KEEP_ALIVE = Utils.getIntegerNetProperty(
            "jdk.httpclient.keepalive.timeout", 1200); // seconds
static final long MAX_POOL_SIZE = Utils.getIntegerNetProperty(
            "jdk.httpclient.connectionPoolSize", 0); // unbounded

jdk.internal.net.http.HttpConnection

從jdk.internal.net.http.HttpConnection中可以看到連接如何放入池中

/**
     * Forces a call to the native implementation of the
     * connection's channel to verify that this channel is still
     * open.
     * <p>
     * This method should only be called just after an HTTP/1.1
     * connection is retrieved from the HTTP/1.1 connection pool.
     * It is used to trigger an early detection of the channel state,
     * before handling the connection over to the HTTP stack.
     * It helps minimizing race conditions where the selector manager
     * thread hasn't woken up - or hasn't raised the event, before
     * the connection was retrieved from the pool. It helps reduce
     * the occurrence of "HTTP/1.1 parser received no bytes"
     * exception, when the server closes the connection while
     * it's being taken out of the pool.
     * <p>
     * This method attempts to read one byte from the underlying
     * channel. Because the connection was in the pool - there
     * should be nothing to read.
     * <p>
     * If {@code read} manages to read a byte off the connection, this is a
     * protocol error: the method closes the connection and returns false.
     * If {@code read} returns EOF, the method closes the connection and
     * returns false.
     * If {@code read} throws an exception, the method returns false.
     * Otherwise, {@code read} returns 0, the channel appears to be
     * still open, and the method returns true.
     * @return true if the channel appears to be still open.
     */
    final boolean checkOpen() {
        if (isOpen()) {
            try {
                // channel is non blocking
                int read = channel().read(ByteBuffer.allocate(1));
                if (read == 0) return true;
                close();
            } catch (IOException x) {
                debug.log("Pooled connection is no longer operational: %s",
                        x.toString());
                return false;
            }
        }
        return false;
    }
void closeOrReturnToCache(HttpHeaders hdrs) {
        if (hdrs == null) {
            // the connection was closed by server, eof
            Log.logTrace("Cannot return connection to pool: closing {0}", this);
            close();
            return;
        }
        HttpClientImpl client = client();
        if (client == null) {
            Log.logTrace("Client released: closing {0}", this);
            close();
            return;
        }
        ConnectionPool pool = client.connectionPool();
        boolean keepAlive = hdrs.firstValue("Connection")
                .map((s) -> !s.equalsIgnoreCase("close"))
                .orElse(true);
        if (keepAlive && checkOpen()) {
            Log.logTrace("Returning connection to the pool: {0}", this);
            pool.returnToPool(this);
        } else {
            Log.logTrace("Closing connection (keepAlive={0}, isOpen={1}): {2}",
                    keepAlive, isOpen(), this);
            close();
        }
    }

以上就是java.net.http.HttpClient使用示例解析的詳細(xì)內(nèi)容,更多關(guān)于java.net.http.HttpClient的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mock和@InjectMocks的區(qū)別及說明

    Mock和@InjectMocks的區(qū)別及說明

    @Mock和@InjectMocks是Mockito框架中的兩個(gè)注解,前者用于創(chuàng)建模擬對(duì)象,后者用于將模擬對(duì)象注入到被測(cè)試類中
    2024-11-11
  • java普通項(xiàng)目讀取不到resources目錄下資源文件的解決辦法

    java普通項(xiàng)目讀取不到resources目錄下資源文件的解決辦法

    這篇文章主要給大家介紹了關(guān)于java普通項(xiàng)目讀取不到resources目錄下資源文件的解決辦法,Web項(xiàng)目中應(yīng)該經(jīng)常有這樣的需求,在maven項(xiàng)目的resources目錄下放一些文件,比如一些配置文件,資源文件等,需要的朋友可以參考下
    2023-09-09
  • 實(shí)例詳解Java中如何對(duì)方法進(jìn)行調(diào)用

    實(shí)例詳解Java中如何對(duì)方法進(jìn)行調(diào)用

    這篇文章主要介紹了實(shí)例詳解Java中如何對(duì)方法進(jìn)行調(diào)用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-10-10
  • Java 使用線程池執(zhí)行多個(gè)任務(wù)的示例

    Java 使用線程池執(zhí)行多個(gè)任務(wù)的示例

    這篇文章主要介紹了Java 使用線程池執(zhí)行多個(gè)任務(wù)的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • 在 Spring Boot 項(xiàng)目中實(shí)現(xiàn)文件下載功能

    在 Spring Boot 項(xiàng)目中實(shí)現(xiàn)文件下載功能

    這篇文章主要介紹了在 Spring Boot 項(xiàng)目中實(shí)現(xiàn)文件下載功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • springboot整合freemarker的踩坑及解決

    springboot整合freemarker的踩坑及解決

    這篇文章主要介紹了springboot整合freemarker的踩坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 三道java新手入門面試題,通往自由的道路--多線程

    三道java新手入門面試題,通往自由的道路--多線程

    這篇文章主要為大家分享了最有價(jià)值的3道多線程面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對(duì)hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java實(shí)現(xiàn)螺旋矩陣的示例

    Java實(shí)現(xiàn)螺旋矩陣的示例

    這篇文章主要介紹了Java實(shí)現(xiàn)螺旋矩陣的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Python中scrapy框架的ltem和scrapy.Request詳解

    Python中scrapy框架的ltem和scrapy.Request詳解

    這篇文章主要介紹了Python中scrapy框架的ltem和scrapy.Request詳解,Item是保存爬取數(shù)據(jù)的容器,它的使用方法和字典類似,不過,相比字典,Item提供了額外的保護(hù)機(jī)制,可以避免拼寫錯(cuò)誤或者定義字段錯(cuò)誤,需要的朋友可以參考下
    2023-09-09
  • Java歸并排序算法代碼實(shí)現(xiàn)

    Java歸并排序算法代碼實(shí)現(xiàn)

    歸并(Merge)排序法是將兩個(gè)(或兩個(gè)以上)有序表合并成一個(gè)新的有序表,即把待排序序列分為若干個(gè)子序列,每個(gè)子序列是有序的,下面這篇文章主要給大家介紹了關(guān)于Java歸并排序算法的相關(guān)資料,需要的朋友可以參考下
    2024-03-03

最新評(píng)論