Java – Getting Binary HTTP Post parameter in an Java/Tomcat/HttpServlet

httpjavatomcat

I have a binary value being URL Encoded, and then POSTed to an HttpServlet. The following code shows how I first attempted to extract this data. Very simple except that the result is a String, not bytes.

This seemed to work at first, except that an extra byte appeared three bytes from the end. What I eventually figured out was that my data was being treated as Unicode and converted from one Unicode encoding to UTF-8.

So, other that getting the entire post body and parsing it myself, how can I extract my data without treating it as a string after the url encoding is decoded? Have I misunderstood the specs for posted data in general, or is this a Java/Tomcat specific issue?

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // Receive/Parse the request
    String requestStr = request.getParameter("request");
    byte[] rawRequestMsg = requestStr.getBytes();

Here is a snippet of the Python test script I'm using for the request:

    urlRequest = urllib.urlencode( {'request': rawRequest} )

    connection = urllib.urlopen(self.url, data = urlRequest)
    result = connection.readlines()
    connection.close()

Best Answer

I think this should work (it treats request as a single-byte encoding, so transformation to String is completely reversible):

String someSingleByteEncoding = "ISO-8859-1";
request.setCharacterEncoding(someSingleByteEncoding);
String requestStr = request.getParameter("request"); 
byte[] rawRequestMsg = requestStr.getBytes(someSingleByteEncoding);