httpClient中文乱码问题解决(wap提交)

我在尝试着直接将中文改变为utf-8的字符串直接写入,失败后!以为是网络传输中应该是iso-8859-1方式传输的,然后将中文转为该编码格式,还是失败后,看httpclient源代码发现:重写postmethod中的getrequestcharset()方法,虽然源代码中该方法动态的设置编码格式,但是好像并没有很好的执行!在重写后,问题解决!
package pro;

import java.io.IOException;

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

public class simulateWebAction {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String url = “*******”;
        PostMethod postMethod = new UTF8PostMethod(url);
        StringBuilder origin = new StringBuilder();
        origin.setLength(0);
       
        HttpClient httpClient = new HttpClient();
//        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
       
        NameValuePair a = new NameValuePair("a","**");
        NameValuePair q = new NameValuePair("q","**");
        NameValuePair[] param = new NameValuePair[]{a,q};
       
        postMethod.setRequestBody(param);
        try {
//            执行getMethod
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: "+ postMethod.getStatusLine());
            }else{
//                读内容
                System.out.println(postMethod.getResponseBodyAsString());
            }
           
            } catch (HttpException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                postMethod.releaseConnection();
            }   
    }
   
    public static class UTF8PostMethod extends PostMethod{
        public UTF8PostMethod(String url){
            super(url);
        }
        @Override
        public String getRequestCharSet() {
            //return super.getRequestCharSet();
            return "UTF-8";
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值