一个Java访问网页时URL重定向的问题

这是去年年底一直想解决的问题(这么简单的问题,现在才解决,说出来有点丢人,唉。。。)。昨天终于知道怎么解决了,原来是就是用了HttpURLConnection类的getHeaderField("location");方法。然后就是简单而又繁琐的拼接URL了,下面是我的项目中自己写的一个方法,大家见笑了。。。

 

public static Entity_Page DownLoadContent(String str) {
//    System.out.println(" str : " + str);
    Entity_Page page = new Entity_Page();
    String content = null;
    URL url = null;
    BufferedReader br = null;
    HttpURLConnection conn = null;
    InputStream is = null;
    String encode = null;
    boolean judge = false;
    String title = "";
    int responsecode;
    StringBuffer sb = null;
    try {
      str = ToChinese(str);
      url = new URL(str);
      conn = (HttpURLConnection) url.openConnection();
//      HttpURLConnection.setFollowRedirects(true);
      conn.setFollowRedirects(true);
      conn.setRequestMethod("GET");
      conn.setRequestProperty("User_Agent",
          "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
      conn.connect();
      responsecode = conn.getResponseCode();
      if (responsecode == 200) {
        is = conn.getInputStream(); //error
        encode = GetEncode(url); //得到该页面的编码
        br = new BufferedReader(new InputStreamReader(is, encode));
        sb = new StringBuffer();
        String line = "";
        while ( (line = br.readLine()) != null) {
          sb.append(line);
        }
        content = sb.toString();
        page.encode = encode; //该网页的编码方式
        page.content = content; //该网页的内容
        page.title = title;
        page.seed = str;
//        page.judge = judge; //是否为完整的网页
      }
      else if (responsecode == 300 || responsecode == 301 ||
               responsecode == 302 || responsecode == 304) {
        String s = str.substring(7);
        String u = null;
        if (s.indexOf('/') != -1) {
          u = "http://" + s.substring(0, s.indexOf('/')) +
              conn.getHeaderField("location");
//          System.out.println("11========== : " + u);
        }
        else {
          u = "http://" + s + conn.getHeaderField("location");
//          System.out.println("22========== : " + u);
        }

        URL uu = new URL(u); //u为最终重定向后的URL
        conn = (HttpURLConnection) uu.openConnection();
        conn.connect();
        InputStream stream = conn.getInputStream();
        encode = GetEncode(uu);
        br = new BufferedReader(new InputStreamReader(stream, encode));
        sb = new StringBuffer();
        String line = "";
        while ( (line = br.readLine()) != null) {
          sb.append(line);
        }
        content = sb.toString();
        page.encode = encode; //该网页的编码方式
        page.content = content; //该网页的内容
        page.title = title;
        page.seed = u; //设置重定向后的URL
      }
      else {
        System.err.println("Connection error : " + responsecode + "  " +
                           url);
        return null;
      }
    }
    catch (Exception ex) {
//      System.out.println("?????????  " + url);
//      ex.printStackTrace(); 需要处理
      System.err.println("Connection timed out : " + url);
    }
    finally {
      try {
        if (sb != null) {
          sb = null;
        }
        if (br != null) {
          br.close();
          br = null;
        }
        if (is != null) {
          is.close();
          is = null;
        }
        if (conn != null) {
          conn.disconnect();
          conn = null;
        }
      }
      catch (IOException ex1) {
        ex1.printStackTrace();
      }
    }
    return page;
  }

 

网上查资料时,都说用HttpClient好使,但我没试验成功。。。还得继续努力呀。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值