httpClient post汉字乱码问题

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class UTF8PostMethod1 {

  public static void main(String[] args) throws Exception, IOException {
         String url = "/product.php";
         PostMethod postMethod = new UTF8PostMethod(url);
         //填入各个表单域的值
         NameValuePair[] data = {
                 new NameValuePair("keyword", "测向仪"),
         };
         //将表单的值放入postMethod中
         postMethod.setRequestBody(data);
         //执行postMethod
         HttpClient httpClient = new HttpClient();
         httpClient.setConnectionTimeout(5000);
         httpClient.getHostConfiguration().setHost("search.5117.com", 80, "http");
         httpClient.executeMethod(postMethod);
//       打印结果页面
         System.out.println(new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1"),"GB2312"));
     }
    
     //Inner class for UTF-8 support
     public static class UTF8PostMethod extends PostMethod{
         public UTF8PostMethod(String url){
             super(url);
         }
         @Override
         public String getRequestCharSet() {
             //return super.getRequestCharSet();
             return "gb2312";
         }
     } 
}

你可能感兴趣的:(apache,PHP)