system.net.httpPost is not binary safe

Using system.net.httpPost to send arbitrary data is not binary safe.

Only tested on 7.8.2-rc1

From the manual:

[quote]You can post arbitrary data as well, but you’ll need to specify the MIME type. The document is then returned as a string.
[/quote]

eg sending a 1x1 pixel png by way of example in the attached txt file shows the corruption of the sent data.

[attachment=0]httpPost.txt[/attachment]

For headers and data as indicated in file

#this works
import urllib2
request = urllib2.Request('http://localhost:8000/add', data=data, headers=headers)
f = urllib2.urlopen(request)
print f.read()

#this corrupts binary data
ret=system.net.httpPost("http://localhost:8000/add", headers['Content-Type'], data)
print ret

The application/octet-stream mimetype is intentional in my application.

Quick inspection seems to indicate that it has something to do with the java byte array being a signed value…eg the corruption appears to occur when the value is negative eg over 7bit.

I experienced the same issue. After hexdumping both my original image and the corrupted one I got from the post I realized that the httpPost call is assuming your input string is ISO-Latin1 and converting it to UTF-8 when it posts it. In order to get around this I base64 encoded the image (file) I was sending and decoded it on the server end. I have to say it took me a while to figure it out but I hope this helps someone with the same or similar issue.

Update
For those interested I attached the hexdump for both the corrupted and non corrupted images. (sample was a 1x1 redish orange pixel)