ITeye博客

使用HTTPCLIENT发送包含中文参数

Jxdwuao · 2010-12-22 20:18 · Java · 编程语言 · Apache · Markdown

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

使用的代码总是不能按照中文搜索, 只能按照英文和数字搜。经过别人过来帮助,在提交时,把参数按照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)

暂无评论。

评论功能维护中,当前仅展示历史评论。