httpclient 获取到网页内容乱码问题
最近在爬一些页面 碰到解析的内容有乱码 最后这个方法解决了这个乱码问题
public static String getHTMLByDeCode(String url, String... params) throws Exception {DefaultHttpClient httpClient = new DefaultHttpClient();int index = 0;if(ipPortList.size() != 0){index = (int) (Math.random() * ipPortList.size() );String ipPort = ipPortList.get(index);if(!StringUtil.isEmpty(ipPort)){logger.debug(index+">>>"+ipPort);String[] ipPortResult = ipPort.split(":");HttpHost proxy = new HttpHost(ipPortResult[0], Integer.parseInt(ipPortResult[1]));//设置代理iphttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);}}HttpProtocolParams.setUserAgent(httpClient.getParams(),"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");String charset = "UTF-8";if (null != params && params.length >= 1) {charset = params[0];}HttpGet httpget = new HttpGet();String content = "";try{httpget.setURI(new java.net.URI(url));HttpResponse response = httpClient.execute(httpget);HttpEntity entity = response.getEntity();if (entity != null) {// 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1content = EntityUtils.toString(entity, charset);httpget.abort();httpClient.getConnectionManager().shutdown();}}catch(Exception e){if(ipPortList.size() != 0)ipPortList.remove(index);e.printStackTrace();logger.debug("get proxy again!!!!");getHTMLByDeCode(url,params);}return content;}