gpt4 book ai didi

java okhttp使用for循环动态添加 header

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:45 27 4
gpt4 key购买 nike

许多示例表明添加 header 应该:

Request request = new Request.Builder()
.url("https://api.github.com/repos/square/okhttp/issues")
.header("User-Agent", "OkHttp Headers.java")
.addHeader("Accept", "application/json; q=0.5")
.addHeader("Accept", "application/vnd.github.v3+json")
.build();

但是我想根据用户的header要求动态添加header,如何实现呢?

Headers h = new Headers.Builder().build();
for (HttpHeader hh : ht.HttpRequestHeader) {
h.newBuilder().add(hh.Name, hh.Value);
} //<<---Nothing changed!!!!!

最佳答案

Headers.Builder builder = new Headers.Builder();
for (HttpHeader hh : ht.HttpRequestHeader) {
builder.add(hh.Name, hh.Value);
}
Headers h = builder.build();

关于java okhttp使用for循环动态添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com