下面五个网地在IE中是可以打开的:
http://www.163.com
http://hi.csdn.net/lishigui
http://www.google.com.hk
http://www.baidu.com
http://www.csdn.net但是用java.net.URLConnection打开则只能打开:
http://www.baidu.com
http://www.csdn.net
其它三个无法打开,这是为什么??
请各位大侠赐教!!代码:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;public class PageDown { public PageDown(String sUrl) {
try {
URL url = new URL(sUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(6000);
urlConnection.setReadTimeout(6000);
String type = urlConnection.getContentType();
System.out.println(type);
urlConnection.connect();
int contentLength = urlConnection.getContentLength();
if (contentLength > 0) {
InputStream raw = urlConnection.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1) {
break;
}
offset += bytesRead;
}
in.close();
raw.close();
System.out.println(new String(data, type.split("charset=")[1]));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
//无法下载
new PageDown("http://www.163.com");
new PageDown("http://hi.csdn.net/lishigui");
new PageDown("http://www.google.com.hk/");
//可以下载
new PageDown("http://www.baidu.com");
new PageDown("http://www.csdn.net/"); }}

解决方案 »

  1.   

    我的好像没问题啊import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.nio.charset.Charset;public class HelloWorld {    public static void main(String[] args) throws IOException {
            URL url = new URL("http://www.google.com.hk");
            URLConnection con = url.openConnection();
            BufferedReader br = null;
            String charset = charset(con.getContentEncoding(), con.getContentType());
            try {
                br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
                for(String str = null; (str = br.readLine()) != null; ) {
                    System.out.println(str);
                }
            } finally {
                if(br != null) {
                    br.close();
                }
            }
        }
        
        private static String charset(String encoding, String contentType) {
            if(encoding != null && Charset.isSupported(encoding)) {
                return encoding;
            }
            if(contentType == null) {
                return Charset.defaultCharset().name();
            }
            int index = contentType.indexOf("charset=");
            if(index < 0) {
                return Charset.defaultCharset().name();
            }
            String charset = contentType.substring(index + 8);
            if(!Charset.isSupported(charset)) {
                return Charset.defaultCharset().name();
            }
            return charset;
        }
    }
      

  2.   

    这个代码除了“http://hi.csdn.net/lishigui”不能打开,其它的都能打开
      

  3.   

    对于google.hk的网页貌似无法获得真实contentLength,导致lz无法获得内容修改后,同样见效于http://hi.csdn.net/lishigui
      

  4.   


    很抱歉!不是http://hi.csdn.net/lishigui而是http://blog.csdn.net/lishigui打不开Server returned HTTP response code: 403 for URL: http://blog.csdn.net/lishigui
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1223)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1217)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:906)
    at com.lsg.htmlparser.PageDown.<init>(PageDown.java:26)
    at com.lsg.htmlparser.PageDown.main(PageDown.java:45)
    Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://blog.csdn.net/lishigui
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1170)
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:1903)
    at java.net.URLConnection.getContentType(URLConnection.java:479)
    at com.lsg.htmlparser.PageDown.<init>(PageDown.java:22)
    ... 1 more