java.net.URISyntaxException: Illegal character in scheme name at index 0:

来源:互联网 发布:视频投稿网站源码 编辑:程序博客网 时间:2024/06/05 10:36

爬虫的时候,遇到一个问题:

java.net.URISyntaxException:  Illegal character in scheme name at index 0:

原始写法:

/* 实例化一个HttpClient客户端 *//*HttpClient client = new DefaultHttpClient();           HttpGet getHttp = new HttpGet(url); */


原因:


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



解决办法:

HttpClient client = new DefaultHttpClient(); HttpGet getHttp = null;try{URL url1 = new URL(url); URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null); getHttp = new HttpGet(uri);}catch(Exception e){e.printStackTrace();}


0 2
原创粉丝点击