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 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块