问题是---------bufferreader不管如何转化字符集读取,内容显示均为乱码;后来,使用htmlparse解析html的开源代码,发现在读取网页时没有问题 ==== trace code 发现 乱码有可能是压缩网页造成
比如是-----gzip格式的网页;所以采用以下代码读取网页是没有问题
public static void GetFromHtml() throws IOException{
//int ret=0;
String contentEncoding;
HttpURLConnection url=null;
//String htmladdr="http://www.zhcw.com/data-js/nowdata50.js";
String htmladdr="http://www.itpub.net/753054.html";
StringBuffer buffer=new StringBuffer("");
try {
URL url1 = new URL(htmladdr);
url = (HttpURLConnection)url1.openConnection ();
url.setRequestProperty("User-Agent", "mozlla/5.0");
url.setRequestProperty("Accept-Encoding", "gzip, deflate");
url.connect();
contentEncoding=url.getContentEncoding();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ;
}
if(url!=null){
InputStream stream;
if ((null != contentEncoding)
&& (-1 != contentEncoding.indexOf ("gzip"))
{
stream = new GZIPInputStream (
url.getInputStream ());
}
else if ((null != contentEncoding)
&& (-1 != contentEncoding.indexOf ("deflate"))
{
stream = new InflaterInputStream (
url.getInputStream ());
}
else
{
stream = url.getInputStream ();
}
//InputStream stream= url.getInputStream();
BufferedReader reader =
new BufferedReader(new InputStreamReader(stream));
String temp;
while (null != (temp = reader.readLine())) {
//temp=new String(reader.readLine().getBytes("gbk");
System.out.println(temp);
// break;
}
reader.close();
}
}
分享到:
相关推荐
HttpURLConnection文件下载\httpURLConnection文件下载
本示例使用HttpUrlConnection实现上传文件,可以将手机上的文件上传到服务器 必须与my_upload工程配合运行,先启动my_upload工程
使用HttpUrlConnection实现上传文件服务器端代码,引用commons-fileupload-1.2.1.jar、commons-io-1.4.jar
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
通过HttpURLConnection把本地目录上的资源打包远程到另一台服务器上,有通过过滤器启动Timer,有手工上传的。
HttpUrlConnection下载文件
NULL 博文链接:https://dreamoftch.iteye.com/blog/1860289
HttpURLConnection实现三方接口HTTP调用调用,带有FILE文件参数
导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下。 正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1。一直想不明白,同一个程序我调用...
Netroid框架 HttpURLConnection类 文件下载demo 主要目的是测量文件下载的速度,注意网络环境的一致性
android客户端上传数据中文乱码3中解决方案 一种HttpPost两种HTTPURLCONNECTION
本文通过实例代码给大家介绍了Android基于HttpUrlConnection类的文件下载功能,非常不错,具有参考借鉴价值,需要的的朋友参考下吧
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();// 取得连接 conn.connect(); InputStream is = conn.getInputStream();//取得返回的InputStream bitmap = BitmapFactory....
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习
jsoup是一个Java HTML Parser。能够从URL、文件或字符串解析HTML。利用DOM traversal或CSS selectors查找和抽取数据。能够操作HTML元素,属性和文本。能够依据一个白名单过滤用户提交的内容。
Android HttpUrlConnection使用方法 post and get json.
java HttpURLConnection示例 HttpURLConnection工具
HttpURLConnection调用三方HTTP接口,带有FILE文件参数