views:

215

answers:

2

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
+2  A: 

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);
wrang-wrang
+2  A: 

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
ZZ Coder