liuzhen231 2016-04-15 03:16 采纳率: 33.3%
浏览 1839

用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)
  • 写回答

2条回答

  • lobster12 2016-04-15 08:59
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型