最近在使用HttpClient抓取京东的数据时。爬取爬到一半突然出现一个错误。如下:

HttpClient抓取数据出现java.net.URISyntaxException: Expected authority at index 8: https://

 

查找了一些网上资料,说地址中涉及了特殊字符,如‘|’‘&’等。所以不能直接用String代替URI来访问。必须采用%0xXX方式来替代特殊字符。但这种办法不直观。所以只能先把String转成URL,再能过URL生成URI的方法来解决问题。代码如下:

URI uri = null;
        try {
            URL url2 = new URL(strUrl);
            uri = new URI(url2.getProtocol(), url2.getHost(), url2.getPath(), url2.getQuery(), null);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
// 创建httpGet请求对象,设置url地址
HttpGet httpGet = new HttpGet(uri);
 

即可解决这个问题

 

相关文章:

  • 2021-11-03
  • 2021-04-11
  • 2021-12-16
  • 2022-01-28
  • 2021-09-15
  • 2021-09-11
  • 2021-12-02
  • 2021-10-22
猜你喜欢
  • 2022-12-23
  • 2021-05-24
  • 2022-12-23
  • 2021-09-15
  • 2021-06-11
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案