2008年11月12日星期三

HttpClient中用POST方式提交中文数据时的解决方案

在用HttpClient编程时,如果用PostMethod提交中文数据,则在服务器端不能得到正确的数据,可以通过下面的方法解决:

1、从PostMethod派生一个类,并改写getRequestCharSet()方法

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

class UTF8PostMethod extends PostMethod {
    public UTF8PostMethod(String url) {
        super(url);
    }

    @Override
    public String getRequestCharSet() {
        // return super.getRequestCharSet();
        return "gb2312";
    }
}

2、在相关的地方,用UTF8PostMethod替换原来的PostMethod类

1 条评论: