最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

java自帶HttpClient使用

在使用java自帶的HttpClient時, 發(fā)現(xiàn)一個現(xiàn)象,即有些以前常用http的header, 是不能夠set到請求里進行傳遞的,在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這幾個, 屬于禁止設置的。

校驗

因為在jdk.internal.net.http.HttpRequestBuilderImpl中, 會進行嚴格的校驗

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

控制連接復用

它自己控制連接的復用,所以設置不了connection這個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使用示例解析的詳細內(nèi)容,更多關于java.net.http.HttpClient的資料請關注腳本之家其它相關文章!

相關文章

  • 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源

    詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源

    這篇文章主要介紹了詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java反射使用的詳細介紹(最新推薦)

    Java反射使用的詳細介紹(最新推薦)

    這篇文章主要介紹了Java反射使用的詳細介紹,反射的第一步都是先得到編譯后的Class類對象,然后就可以得到Class的全部成分,本文結合實例代碼詳細講解,需要的朋友可以參考下
    2023-02-02
  • mybatisplus邏輯刪除基本實現(xiàn)和坑點解決

    mybatisplus邏輯刪除基本實現(xiàn)和坑點解決

    這篇文章主要介紹了mybatisplus邏輯刪除基本實現(xiàn)和坑點解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot中的異常處理與參數(shù)校驗的方法實現(xiàn)

    SpringBoot中的異常處理與參數(shù)校驗的方法實現(xiàn)

    這篇文章主要介紹了SpringBoot中的異常處理與參數(shù)校驗的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Java并發(fā)編程Lock?Condition和ReentrantLock基本原理

    Java并發(fā)編程Lock?Condition和ReentrantLock基本原理

    這篇文章主要介紹了Java并發(fā)編程Lock?Condition和ReentrantLock基本原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Java SpringMVC異步處理詳解

    Java SpringMVC異步處理詳解

    這篇文章主要介紹了Java springmvc的處理異步,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • RocketMQ源碼解析broker?啟動流程

    RocketMQ源碼解析broker?啟動流程

    這篇文章主要為大家介紹了RocketMQ源碼解析broker啟動流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • SpringBoot中的文件上傳與下載詳解

    SpringBoot中的文件上傳與下載詳解

    這篇文章主要介紹了SpringBoot中的文件上傳與下載詳解,springboot是spring家族中的一個全新框架,用來簡化spring程序的創(chuàng)建和開發(fā)過程,本文我們就一起來看看上傳與下載的操作,需要的朋友可以參考下
    2023-08-08
  • MyBatis動態(tài)SQL之<choose><when><otherwise>標簽的使用

    MyBatis動態(tài)SQL之<choose><when><otherwise>標簽的使用

    MyBatis中動態(tài)語句choose-when-otherwise 類似于Java中的switch-case-default語句,本文就來介紹一下MyBatis動態(tài)SQL之<choose><when><otherwise>標簽的使用,感興趣的可以了解一下
    2023-09-09
  • java中文分詞之正向最大匹配法實例代碼

    java中文分詞之正向最大匹配法實例代碼

    中文分詞應用很廣泛,網(wǎng)上也有很多開源項目,下面這篇文章主要給大家介紹了關于java中文分詞之正向最大匹配法的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-11-11

最新評論

南乐县| 合肥市| 始兴县| 都昌县| 绥滨县| 耿马| 福安市| 且末县| 扶绥县| 白河县| 南丰县| 屏南县| 富宁县| 泗洪县| 白银市| 酉阳| 增城市| 黄骅市| 北碚区| 清苑县| 铜山县| 夏邑县| 吉林省| 郸城县| 阜城县| 乡宁县| 南京市| 白朗县| 陆良县| 岫岩| 怀柔区| 浮山县| 航空| 永济市| 萨迦县| 三原县| 惠来县| 福贡县| 祁东县| 邳州市| 邮箱|