How to deal with the URISyntaxException

后端 未结 10 2057
攒了一身酷
攒了一身酷 2020-11-28 09:22

I got this error message :

java.net.URISyntaxException: Illegal character in query at index 31: http://finance.yahoo.com/q/h?s=^IXIC

10条回答
  •  渐次进展
    2020-11-28 09:35

    Rather than encoding the URL beforehand you can do the following

    String link = "http://example.com";
    URL url = null;
    URI uri = null;
    
    try {
       url = new URL(link);
    } catch(MalformedURLException e) {
       e.printStackTrace();
    }
    
    try{
       uri = new URI(url.toString())
    } catch(URISyntaxException e {
       try {
            uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
                          url.getPort(), url.getPath(), url.getQuery(), 
                          url.getRef());
       } catch(URISyntaxException e1 {
            e1.printStackTrace();
       }
    }
    try {
       url = uri.toURL()
    } catch(MalfomedURLException e) {
       e.printStackTrace();
    }
    
    String encodedLink = url.toString();
    

提交回复
热议问题