2 liuzhen231 liuzhen231 于 2016.04.15 11:16 提问

用java写了一个获取页面内容的方法,报错返回521码

我的代码如下:
strUrl = "http://www.tlnews.cn/dzb/tlrb/html/2016-04/15/node_164.html";
public static String getUrlStr(String strUrl, String charSet){
String urlStr = "";
try {
URL url = new URL(strUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
uc.connect();

        InputStream is = uc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, charSet);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer strs = new StringBuffer();
        String str;
        while ((str = br.readLine()) != null) {
            strs.append(str + "\r\n");
        }
        urlStr = strs.toString();
        isr.close();
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return urlStr;
}
一开始是报403错误,后来加上了uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");这句,开始报521错误。我要抓取的链接是http://www.tlnews.cn/dzb/tlrb/html/2016-04/15/node_164.html
错误信息:
java.io.IOException: Server returned HTTP response code: 521 for URL: http://www.tlnews.cn/dzb/tlrb/html/2016-04/14/node_164.html
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at wy.base.test.TestURL.getUrlStr(TestURL.java:32)
at wy.base.test.TestURL.main(TestURL.java:14)

3个回答

qq_24334403
qq_24334403   2016.04.15 16:59

远程连接问题?真奇怪、、、、、、

qq_24334403
qq_24334403   2016.04.15 17:04

检查检查你的头信息,不设置会有默认的吧

CSDNXIAOD
CSDNXIAOD   2016.04.19 15:34

JAVA方法中通过调用URL来获取其返回的内容
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!