Jan 09

HttpClient looks very nice, as it supports many HTTP actions. I tried the webpage download code, but encountered an encoding issues with Chinese web pages.

Language encoding is always a problem for non-English developers, especially Asian developers, whose languages are encoded in 2 bytes for each character.

After playing with the source code for hours, I finally googled a very simple solution. Here it comes:

  1.  
  2. HttpClient client = new HttpClient();
  3. HttpMethod method = new GetMethod(url);
  4. method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
  5. // …. set other parameters
  6. try {
  7.         int statusCode = client.executeMethod(method);
  8.         // other operations
  9. }
  10. catch{
  11.         // exception handling
  12. }
  13.  

Though spent hours, I am very glad that the solution is very simple.

  • Share/Bookmark