Can Java omit sending some headers to HTTP requests?

I am using the HttpConnection class in Java to send HTTP requests.
How to omit unwanted HTTP headers? as:

  • User agent
  • To accept
  • Accept language
  • Accept-encoding
  • Accept-charset
  • Keep-alive
  • Connection
  • Referer
  • If-Modified-Since
+3
source share
2 answers

If you are talking about HttpURLConnection, you cannot do this. Once the header is set, it cannot be deleted.

Setting the header to zero or blank does not work. I tried this before in Java 5, this led to invalid HTTP headers like

Content-Type: text/html
User-Agent
Content-Length: 123
+4
source

, setRequestProperty URLConnection

import java.net.URL;
import java.net.URLConnection;
URL url = new URL("http://www.example.com");
URLConnection urlc = url.openConnection();
urlc.setRequestProperty("User-Agent", null);
+2

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


All Articles