httpClient 4.0.1处理乱码

 利用httpClient下载页面,先利用浏览器查询页面源代码得知页面编码为utf-8,但生成字符串后总是为乱码,经过查询得知,在设置httpGet参数时,设置了接受压缩类型为Gzip,但却没有对其进行解压缩。解压后在生成字符串,或者这是接受类型为空(即不压缩,效率较低)即可。

httpGet.setHeader("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
//		httpGet.setHeader("Accept-Encoding", "gzip");  //此行注释掉即可!!
		httpGet.setHeader("Connection", "keep-alive");
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if( statusCode!= HttpStatus.SC_OK && statusCode != HttpStatus.SC_MOVED_TEMPORARILY)
				throw new NullInfoException();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

                httpClient = new DefaultHttpClient();
		HttpResponse response = MyHttp.getResponse(infoUrl, httpClient);
		HttpEntity entity = response.getEntity();
		String entityBody = null;
		try {
			byte[] bytes = EntityUtils.toByteArray(entity);
			entityBody = new String(bytes, this.charset); //utf-8
			// entityBody = EntityUtils.toString(entity, this.charset);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

你可能感兴趣的:(httpclient,乱码)