博主在使用httpclient抓取含有中文的url时,刚开始使用URLEncoder.encode()去转码,仍然出UnknownHostException异常,通过网上查找,像修改本地host文件之类的方法都不适合大量抓取网页文件的场合使用。最后大牛同事使用java.net.IDN把url用toASCII转码,成功访问地址。
httpClient = HttpClients.createDefault();
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(url);
HttpGet httpGet = null;
if(m.find()){
httpGet = new HttpGet(IDN.toASCII(url));
}else {
httpGet = new HttpGet(url);
}
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpGet.setConfig(requestConfig);
try {
httpReponse = httpClient.execute(httpGet);
} catch (ClientProtocolException e) {
flag++;
e.printStackTrace();
} catch (IOException e) {
flag++;
e.printStackTrace();
}