Tuesday, January 18, 2011

No urlencode resulted in HTTP 505 error

HTTP GET always need URL encoding the request, one typical case is space should get encoded to %20. IE will do the encoding automatically, but when you program using httpclient in Java, do not forget to do this, otherwise you will possibly get 505 error (HTTP Version Not Supported)

Here is one example:
Without URL encoding (505 code)
GET /context/api.do?token=encrypted&p1=xxx&p2=xxx xx HTTP/1.1

With URL encoding (200 code)
GET /context/api.do?token=encrypted&p1=xxx&p2=xxx%20xx HTTP/1.1

In JDK, there is a URLEncoder utility class to provide methods for URL encode and decode.

Note that because the <space> character is very commonly used, a special code ( the "+" sign) has been reserved as its URL encoding.  Thus the string "A B" can be URL encoded as either "A%20B" or "A+B". Therefore in JDK, it was converted to plus sign (+).

Here are excerpts from SUN javadoc
When encoding a String, the following rules apply:

  • 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 "+".
  • 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.

No comments:

Post a Comment