GET/POST传参时间字符串:Server returned HTTP response code: 505 for URL
1.问题描述
在使用HTTPutil发送get/post的请求时,使用URL 读取http流时,出现505异常,反而将该请求直接post或者get的时候反而不会出现错误,详细对比地址会发现,浏览器给我们把具体请求参数中的空格进行了处理:
2.分析
经检查,发现时URL的参数带有空格时,抛出的该异常,即上传参数startDate和endDate的值形如:xxxxxx xxxxxxx"
3.解决方案
对可能含有空格字符的参数进行URL编码,使用java.net.URLEncoder类的的encode()方法对字符串进行编码。
修改后的代码如下:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
String url = "http://XXXXX:8040/sy/d/t1";
JSONObject param = new JSONObject();
param.put("vn", cto.getVn());
param.put("pageNum", page.getCurrent());
param.put("pageSize", page.getSize());
*try{
param.put("startDate", URLEncoder.encode(cto.getStartDate(), "utf-8"));
param.put("endDate", URLEncoder.encode(cto.getEndDate(), "utf-8"));
}catch(UnsupportedEncodingException ee){
ee.printStackTrace();
}*
String result = HttpUtil.sendGet(url,param);
在处理过程中,对时间参数进行异常处理,避免报错
问题解决