1.[代码][JavaScript]代码
public class InternetTest2 {
public static void main(String[] args) throws Exception{
String result = "";
URL url = new URL("http://www.zwdz8.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6* 1000);//设置连接超时
if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();//得到网络返回的输入流
if("gzip".equals(conn.getContentEncoding())){
result = new InternetTest2().readDataForZgip(is, "GBK");
}else {
result = new InternetTest2().readData(is, "GBK");
}
conn.disconnect();
System.out.println(result);
System.err.println("ContentEncoding: " + conn.getContentEncoding());
}
//第一个参数为输入流,第二个参数为字符集编码
public static String readData(InputStream inStream, String charsetName) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return new String(data, charsetName);
}
//第一个参数为输入流,第二个参数为字符集编码
public static String readDataForZgip(InputStream inStream, String charsetName) throws Exception{
GZIPInputStream gzipStream = new GZIPInputStream(inStream);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer =new byte[1024];
int len = -1;
while ((len = gzipStream.read(buffer))!=-1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
gzipStream.close();
inStream.close();
return new String(data, charsetName);
}
}