在url中包含(‘|’‘&’)这些字符直接用HttpClient请求就会报上述错误,
在网上看了一下解决办法有两种都是将非法字符进行转码再请求
一、
import java.net.URI;
import java.net.URL;
URL url1 = new URL(url);
URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null);
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(uri);
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
二、
import java.net.URLEncoder;
String path = URLEncoder.encode(strUrl,"utf-8");
个人比较偏向第一种方法,因为第二种转义会将url中所有的字符都转义,导致请求错误
参考:https://blog.csdn.net/qq_34972627/article/details/74908653
https://www.cnblogs.com/qianzf/p/8541925.html