转载请注明来源-作者@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
至此,异常排除!
若你在开发过程中也碰到类似问题,但上述方法不适用,请留言,可以一起看看。
版权声明:本文为loongshawn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。