gpt4 book ai didi

java - 如何在 Java 中向 http 请求方法添加新 header ?

转载 作者:行者123 更新时间:2023-12-02 11:46:20 25 4
gpt4 key购买 nike

我需要在我的服务器中为请求实现特殊的 header (以及一些新的响应)。让我们这样说:

GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
My-header-goes-here: example-value

如您所见,我想通过 My-header-goes-here 扩展 GET 的标准版本。我怎样才能实现这一目标?

最佳答案

try {
URL url = new URL("myserver.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("My-header-goes-here", "example-value");
connection.setConnectTimeout(16000);
connection.setReadTimeout(16000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(json);
out.close();

return connection.getResponseCode();
} catch (Exception e) {

}

关于java - 如何在 Java 中向 http 请求方法添加新 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176222/

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