java 解决HttpGet出现Illegal character in query异常

问题代码(编译没问题):

String strUrl="http://a.cn/send?a=1&b=1|3&mobile=%s&content=%s";
strUrl=String.format(strUrl, encryptMobile, content);
HttpGet httpGet = new HttpGet(strUrl);

异常现象:
java 解决HttpGet出现Illegal character in query异常_第1张图片
原因:
url址址中存在特殊字符“|”

解决方案:

String strUrl="http://a.cn/send?a=1&b=1|3&mobile=%s&content=%s";
strUrl=String.format(strUrl, encryptMobile, content);
//增加代码 begin
URL url = new URL(strUrl);			
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
//增加代码 end
HttpGet httpGet = new HttpGet(uri);

你可能感兴趣的:(java)