如何更改 Java 中的默认 HTTP OPTIONS 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2196013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:59:49  来源:igfitidea点击:

How to change the default HTTP OPTIONS parameters in Java

javahttphttp-headers

提问by nzpcmad

My java snippet looks like:

我的 java 片段看起来像:

...
String type = "text/plain;charset=UTF-8";
URL url = new URL("http://xxx/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
conn.setRequestMethod("OPTIONS");
conn.setRequestProperty("Content-Type", type);
...

When I sniff what this sends it sends a

当我嗅到它发送的内容时,它会发送一个

OPTIONS / HTTP/1.1

选项 / HTTP/1.1

which appears to be the default.

这似乎是默认设置。

However, I actually want to send

但是,我实际上想发送

OPTIONS * HTTP/1.0

选项 * HTTP/1.0

How would I do this?

我该怎么做?

采纳答案by BalusC

You can't do that with "plain" java.net.URLConnection. Consider replacing by Apache Commons HttpClientwhich is less bloated and more configureable. You can force HTTP 1.0 mode by setting http.protocol.versionto HttpVersion.HTTP_1_0in HttpClient#getParams(). You can find an example in this document.

你不能用 "plain" 做到这一点java.net.URLConnection。考虑用Apache Commons HttpClient替换,它不那么臃肿且更易于配置。您可以通过设置http.protocol.versionHttpVersion.HTTP_1_0in来强制 HTTP 1.0 模式HttpClient#getParams()。您可以在本文档中找到示例。

回答by Dilip Rajkumar

I agree with the answer the following is the code using HTTPClient

我同意答案以下是使用 HTTPClient 的代码

HttpClient client = new DefaultHttpClient(); 
            client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);

Hope it helps some one..

希望它可以帮助某人..