A users asks:
RE: "I wish to post a form in java that has both string and binary parameters e.g.
Unfortunately the available documentation only covers uploading either strings or binary data separately. How can I combine the two?"
java.net.URL is no fun to work with either. I wrote a utility called HTTP that I was using to post REST calls w/o including Apache Commons HTTPRequest (not my thing). HTTP uses java.net.URL. I added a new form poster and my own URLEncoder for byte arrays. I included it below. You can cut and paste it or use boon (I don't care, not looking for boon converts, but I troll stackoverflow looking for things that people do, and add it too boon for practice)
Basically you need to send the mime-type `application/x-www-form-urlencoded'. The fields have to be text.
The field names and values are escaped/encoded, e.g., space characters are replaced by +', reserved characters are escaped using URL encoding. Oh and that is not all... Non-alphanumeric characters are replaced by%HH' as in %20 for space (but space is + so)....
So two hexadecimal digits representing the ASCII code of the character. If only Java could some how do this for you..... Oh wait it can... But is is a new class. It has only been around since Java 1.0. Check out URLEncoder, it is a Utility class for HTML form encoding. But it does not work with bytes like you want... :)
URLEncoder, included with the JDK, contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. You can learn more about HTML form encoding, by consulting the HTML specification (cited below). Also check out: http://docs.oracle.com/javase/1.4.2/docs/api/java/net/URLEncoder.html
The URLEncoder handles the following: "The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same. The special characters ".", "-", "*", and "_" remain the same." The space character " " is converted into a plus sign "+". "
Here is the kicker for binary conversion...
"All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used."
Specify UTF-8 always.
Here is the HTTP specification.
This actually seems challenging enough to try to tackle myself, so let's do it.