使用HTTPCLIENT发送包含中文参数

在公司使用中文搜索功能,调用的是商城的接口 。

使用的代码总是不能按照中文搜索, 只能按照英文和数字搜。经过别人过来帮助,在提交时,把参数按照UTF-8编码,解决问题。

代码:

 

String  url = ConfigurationUtils.getProperitisValue("getGoodList");
        JSONObject paramJson = getPageJsonParam();
        
        String urlParams="";
		try {
			//把参数按照UTF-8格式编码
			urlParams = URLEncoder.encode(paramJson.toString(), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        String resultStr = HttpClientUtils.httpClientExecuteMethod(url + "&$$CDORequest$$=" + urlParams );
        JSONObject result=JSONObject.fromObject(resultStr);

/**
	 * 商城接口访问 
	 * @param url
	 * @return
	 */
	public static String httpClientExecuteMethod(String url) {
		int state =0;
		HttpMethod method=null;
		try {
			org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
			method= new GetMethod(url);
			state = client.executeMethod(method);
			return method.getResponseBodyAsString();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


    /**
     * 获取分页相关的商品分类参数
     * @return
     */
    public JSONObject getPageJsonParam(){
        JSONObject paramJson = new JSONObject();
        paramJson.put("cdosSortConditon",getOrderJSonArray());
        //加入搜索关键字的参数_判断是否有前缀,如果有的话就加搜索关键字
        String searchWordParam = this.request.getParameter("strKeywords");
        if(!MallUtils.strIsNull(searchWordParam) && StringExtUtils.containsIgnoreCase(searchWordParam, this.searchWordPre)){
            String searchWord = (String) searchWordParam.substring(this.searchWordPre.length() , searchWordParam.length());
            paramJson.put("strKeywords",searchWord); 
        } 
        
        String[] requestParamKeys = {"lChannelCategoryId","nChannelCategoryLevel" , "nMinPrice" , "nMaxPrice" , "nPageIndex" , "nPageSize" ,"lBrandId"};
        return  injectBasicParam(paramJson ,requestParamKeys);
    }


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值