java.lang.IllegalArgumentException: Illegal character in query at index ...解决办法

18 篇文章 0 订阅

转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/70141420,建议读者阅读原文,确保获得完整的信息

首先看看http请求的代码:

protected String doHttp(String req,String url) throws Exception{

        // Create the POST object and add the parameters
        String URL = url + req;
        HttpPost httpPost = new HttpPost(URL);
        StringEntity entity = new StringEntity(URL, "UTF-8");
        entity.setContentType("application/json;charset=UTF-8");
        httpPost.setEntity(entity);
        HttpClient client = HttpClients.createDefault();

        // Execute POST
        HttpResponse response = client.execute(httpPost);

        // Get Response
        HttpEntity httpEntity = response.getEntity();

        // Define Return
        String data = "";

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            data = StreamUtils.convertStreamToString(inputStream);
            logger.info("response串: " + data);
        }

        return data;
}

上述方法实现POST请求,用例请求的参数以表单方式传输,形如:

http://101.123.45.107:8080/user/web/Need?data={"ali_gh":"123","att_path":"123","content":"测试","context":"测试","email_cc":"loongshawn@loongshawn.com","email_to":"loongshawn@loongshawn.com","finish_time":"2017-04-20","m_pj_code":"123","m_work_code":"123","name":"开放需求","presenter":"小明","workload":200}

起初,doHttp方法中req传入的参数是原始的字符串,未经任何形式的转码:

?data={"ali_gh":"123","att_path":"123","content":"测试","context":"测试","email_cc":"loongshawn@loongshawn.com","email_to":"loongshawn@loongshawn.com","finish_time":"2017-04-20","m_pj_code":"123","m_work_code":"123","name":"开放需求","presenter":"小明","workload":200}

请求时,报如下异常:

2017-04-12-11-31 [http-nio-7000-exec-1] [com.autonavi.httpactions.base.ReqBaseNewAction] [INFO] - ReqBaseNewAction doHttpForObject: java.lang.IllegalArgumentException: Illegal character in query at index 64: http://101.123.45.107:8080/user/web/Need?data={"ali_gh":"123","att_path":"123","content":"测试","context":"测试","email_cc":"loongshawn@loongshawn.com","email_to":"loongshawn@loongshawn.com","finish_time":"2017-04-20","m_pj_code":"123","m_work_code":"123","name":"开放需求","presenter":"小明","workload":200}

这里写图片描述

以上异常是由于req未进行URLEncoder转码,故需要对其进行处理,这里有必要说明一下,最终请求的URL是拼接出来的(url + req),这里仅需要对后面的req进行转码即可,新增方法:

public String getTaskCreateJsonReq(JSONObject json){

        StringBuffer req = new StringBuffer();
        try {
            String encode = URLEncoder.encode(json.toJSONString(), "UTF-8");
            req.append("?data=" +encode);
        } catch (UnsupportedEncodingException e){
            req.append("?data=");
            logger.error("TaskReqUtils.getTaskCreateReq [JSONObject]: " + json.toString());
        }

        logger.info("TaskReqUtils.getTaskCreateJsonReq [JSONObject]: " + json.toString());

        return req.toString();
}

URLEncoder.encode后的编码如下

data=%7B%22ali_gh%22%3A%2293102%22%2C%22att_path%22%3A%22http%3A%2F%2FXXXX%2Fattachment%2F201704%2F06%2F20170406163542%2F20170406Penguins.jpg%22%2C%22content%22%3A%22%E6%B5%8B%E8%AF%95%22%2C%22context%22%3A%22%E6%B5%8B%E8%AF%95%22%2C%22email_cc%22%3A%22loongshawn%40loongsahwn.com%22%2C%22email_to%22%3A%22loongshawn%40loongsahwn.com%22%2C%22finish_time%22%3A%222017-04-20%22%2C%22m_pj_code%22%3A%222102002%22%2C%22m_work_code%22%3A%222102%22%2C%22name%22%3A%22%E8%BD%A6%E5%8E%82%E9%9C%80%E6%B1%82%22%2C%22presenter%22%3A%22%E5%B0%8F%E6%98%8E%22%2C%22workload%22%3A200%7D

至此,异常排除!

若你在开发过程中也碰到类似问题,但上述方法不适用,请留言,可以一起看看。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值