breadbin
breadbin

Reputation: 584

Android HttpUrlConnection POSTing an image - What should content length refer to?

I realise this question is similar to this one but I think mine is a different case, as I am trying to POST an image.

I'm uploading an image to a server from an Android activity, and I'm getting an error report of "411 - Length required" when uploading on some devices. I can't reproduce the error here, but a user has reported this error on a different device from the ones we've been able to test on, in a different country.

We open a HttpUrlConnection on a URL with a few parameters attached. These are Strings and UUIDs. It looks like this:

http://ourdomainname.net/OurMobileServices/ImageUploadhandler.ashx?filename=image20121220_120734_-901992324.jpeg&username=testusername&note=&uploadid=b4a1c35b-b869-4f7e-a645-6a4a180f0985&taskid=7f02deb8-c078-45c4-bc37-99e418e8cdf9

Then we use getOutputStream to get an OutputStream, to which we write the image file bytes.

I have been looking for info on the 411 error and setting content length on the HttpUrlConnection object. It seems like I can set content length as a request property, with something like:

connection.setRequestProperty("Content-Length", String.valueOf(<mycomputedInt>));

Or I could use:

connection.setFixedLengthStreamingMode() 

With the content length number in there.

My question is: What should content length be measuring? Is it referring to:

a) the entire URL including parameters?

b) the URL-encoded parameters?

c) the length of the byte array for the image I'm uploading?

d) the length of the byte array for the image, and the URL-encoded parameters?

e) something else entirely?

I can post code or more detail if needed - I'm just pretty confused on this one, and unable to reproduce the error locally. I can upload images just fine from the emulator, a phone, and a tablet. Any help would be greatly appreciated, thanks.

Upvotes: 2

Views: 998

Answers (1)

1615903
1615903

Reputation: 34733

Content-Length should be the length of whatever it is you are writing to the OutputStream. So in this case, the answer is C.

I think this site explains the structure of HTTP requests quite clearly, you may find it helpful.

Upvotes: 2

Related Questions