Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java omit sending some headers in HTTP requests?

I'm using the HttpConnection class in Java to send HTTP requests.
How do I omit unwanted HTTP headers? like:

  • User-Agent
  • Accept
  • Accept-Language
  • Accept-Encoding
  • Accept-Charset
  • Keep-Alive
  • Connection
  • Referer
  • If-Modified-Since
like image 337
Kevin Boyd Avatar asked Sep 19 '09 01:09

Kevin Boyd


2 Answers

If you are talking about HttpURLConnection, you can't do it. Once the header is set, it can't be removed.

Setting header to null or empty doesn't work. I tried this before on Java 5, it resulted invalid HTTP headers, like

Content-Type: text/html
User-Agent
Content-Length: 123
like image 50
ZZ Coder Avatar answered Sep 30 '22 02:09

ZZ Coder


Yes, setRequestProperty in 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);
like image 33
Jonathan Graehl Avatar answered Sep 30 '22 02:09

Jonathan Graehl