14

I am talking to a file upload service that accepts post data, not form data. By default, java's HttpURLConnection sets the Content-Type header to application/x-www-form-urlencoded. this is obviously wrong if i'm posting pure data.

I (the client) don't know the content type. I don't want the Content-Type header set at all. the service has a feature where it will guess at the content type (based on the file name, reading some data from the file, etc).

How do I unset a header? There's no remove header, and setting it to null doesn't change the value and setting it to the empty string results in the header being set with no value.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134

4 Answers4

1

I haven't tested this approach but you can try this:

Extend HttpURLConnection and try by overriding its getContentHandler() and setContentHandler(...) methods. Most probably this should work as, you will take a look at code of getContentHandler().

Prateek
  • 523
  • 2
  • 13
0

Use Apache HttpClient instead of URLConnection

Use fluent Request to generate your request

use removeHeader()

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • not an option, as i need to produce a library that can run on android and plain JSE. the problem w/ android is that it bundles a 4.x version of httpclient that doesn't match with any standalone released version. if you try to bundle a specific version of httpclient, you get conflicts. – Jeffrey Blattman Jan 17 '13 at 16:56
  • I try to implement a bot which using Google Image Search (not using custom search), and Google Image Search seems does not send `Content-Type` and `Content-Length` headers, so I have same issue. And HttpClient of apache httpcomponents does not fit my case too: HttpClient does not support SOCKS proxy (need a tweak, and I don't like that). – LiuYan 刘研 Dec 05 '16 at 17:48
0

I just accomplished this by setting the header to null.

connection.setRequestProperty(MY_HEADER, null);
Jeremythuff
  • 1,518
  • 2
  • 13
  • 35
  • For User-Agent, that just additionally writes `User-Agent` with no colon or value, and then writes the original value nevertheless. Not ideal. – AyCe Aug 05 '21 at 16:51
-1

What do you mean "i don't want the Content-Type header to set at all"?

The browser (or other http client) sends your post request to the server, so it has to inform the server which way it encoded the parameters.

If the Content-Type header is not set, on the server side you (= your server) won't be able to understand how to parse the received data.

If you didn't set Content-Type, the default value will be used.

You browser (or other http client) MUST do two things:

  1. Send key/value pairs.
  2. Inform the server how the key/value pairs were encoded.

So, it is impossible to completely get rid of this header.

Alex Kreutznaer
  • 1,170
  • 8
  • 18
  • i'm not sending parameters. it's file upload. i'm posting binary data. the service saves the binary content as-is. the only reason it needs content type is to set meta data for the file. – Jeffrey Blattman Jan 17 '13 at 16:55
  • 1
    If you upload a image file to Google Image Search, the browser does not send `Content-Type` header, and `Content-Length` header is missing too. – LiuYan 刘研 Dec 05 '16 at 17:51