How to change default HTTP settings in Java

My java fragment looks like this:

... 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 sends, he sends

OPTIONS / HTTP / 1.1

which is the default value.

However i really want to send

OPTIONS * HTTP / 1.0

How can I do it?

+4
source share
2 answers

You cannot do this with the "plain" java.net.URLConnection . Consider replacing the Apache Commons HttpClient , which is less bloated and more customizable. You can force HTTP 1.0 mode by setting http.protocol.version to HttpVersion.HTTP_1_0 in HttpClient#getParams() . You can find an example in this document .

+3
source

I agree with the answer to the following code using HTTPClient

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

Hope this helps someone.

+2
source

Source: https://habr.com/ru/post/1300242/


All Articles