代码之家  ›  专栏  ›  技术社区  ›  Amit

Java -如何找到URL的重定向URL?

  •  58
  • Amit  · 技术社区  · 16 年前

    我通过Java访问网页如下:

    URLConnection con = url.openConnection();
    

    但在某些情况下,URL会重定向到另一个URL。所以我想知道上一个URL重定向到的URL。

    下面是作为响应得到的标题字段:

    null-->[HTTP/1.1 200 OK]
    Cache-control-->[public,max-age=3600]
    last-modified-->[Sat, 17 Apr 2010 13:45:35 GMT]
    Transfer-Encoding-->[chunked]
    Date-->[Sat, 17 Apr 2010 13:45:35 GMT]
    Vary-->[Accept-Encoding]
    Expires-->[Sat, 17 Apr 2010 14:45:35 GMT]
    Set-Cookie-->[cl_def_hp=copenhagen; domain=.craigslist.org; path=/; expires=Sun, 17     Apr 2011 13:45:35 GMT, cl_def_lang=en; domain=.craigslist.org; path=/; expires=Sun, 17 Apr 2011 13:45:35 GMT]
    Connection-->[close]
    Content-Type-->[text/html; charset=iso-8859-1;]
    Server-->[Apache]
    

    因此,目前,我正在从 Set-Cookie 标题字段。在上述情况下,重定向的URL是 copenhagen.craigslist.org

    是否有任何标准方法可以确定特定URL将重定向到哪个URL?

    我知道当一个URL重定向到其他URL时,服务器会发送一个包含 Location 头字段,指示重定向的URL,但我没有通过 url.openConnection(); 方法。

    6 回复  |  直到 8 年前
        1
  •  52
  •   BalusC    16 年前

    你需要把 URLConnection HttpURLConnection 并指示它 通过设置遵循重定向 HttpURLConnection#setInstanceFollowRedirects() false . 也可以通过以下方式全局设置 HttpURLConnection#setFollowRedirects() .

    那么您只需要自己处理重定向。通过以下方式检查响应代码 HttpURLConnection#getResponseCode() 抓住 Location 报头 URLConnection#getHeaderField() 然后对其启动一个新的HTTP请求。

        2
  •  83
  •   Arne Mertz    12 年前

    在调用getinputstream()后,只需对urlConnection实例调用geturl():

    URLConnection con = new URL( url ).openConnection();
    System.out.println( "orignal url: " + con.getURL() );
    con.connect();
    System.out.println( "connected url: " + con.getURL() );
    InputStream is = con.getInputStream();
    System.out.println( "redirected url: " + con.getURL() );
    is.close();
    

    如果您需要知道重定向是否在实际获取其内容之前发生,下面是示例代码:

    HttpURLConnection con = (HttpURLConnection)(new URL( url ).openConnection());
    con.setInstanceFollowRedirects( false );
    con.connect();
    int responseCode = con.getResponseCode();
    System.out.println( responseCode );
    String location = con.getHeaderField( "Location" );
    System.out.println( location );
    
        3
  •  7
  •   محسن عباسی    8 年前
    public static url getfinalurl(url url){
    尝试{
    httpurlConnection con=(httpurlConnection)url.openConnection();
    con.setInstanceFollowerDirects(错误);
    con.setrequestproperty(“用户代理”,“Mozilla/5.0(Windows NT 10.0;Win64;X64)AppleWebKit/537.36(khtml,类似gecko)Chrome/62.0.3202.94 Safari/537.36”);
    con.addrequestproperty(“接受语言”,“en-us,en;q=0.8”);
    con.addrequestproperty(“referer”,“https://www.google.com/”);
    CON.连接();
    //con.getinputstream();
    int rescode=con.getResponseCode();
    if(rescode==httpurlconnection.http_参见其他
    || rescode==httpurlconnection.http_moved_perm
    || rescode==httpurlconnection.http_moved_temp){
    字符串位置=con.getHeaderField(“位置”);
    if(location.startswith(“/”)){
    location=url.getprotocol()+“:/”+url.gethost()+位置;
    }
    返回getfinalurl(new url(location));
    }
    }捕获(异常E){
    system.out.println(e.getmessage());
    }
    返回URL;
    }
    < /代码> 
    
    

    要自己获得“用户代理”和“引用者”,只需转到某个已安装浏览器的开发人员模式(例如,在Google Chrome上按F12)。然后转到“网络”选项卡,然后单击其中一个请求。你应该看看细节。只需按“标题”子选项卡(下图)

    得到“ 用户代理 “和” 引用者 “你自己,只需进入一个已安装浏览器的开发模式(例如,在Google Chrome上按F12)。然后转到“网络”选项卡,然后单击其中一个请求。你应该看看细节。只需按“标题”子选项卡(下图) request details

        4
  •  1
  •   b_erb    16 年前

    看看 HttpURLConnection API documentation 特别是 setInstanceFollowRedirects() .

        5
  •  0
  •   Raymond Kroeker    16 年前

    实际上,我建议使用一个可靠的开源库作为HTTP客户机。如果你看看 http client 通过ASF,你会发现生活更轻松。它是一个易于使用、可扩展和健壮的HTTP客户端。

        6
  •  -3
  •   franzu    10 年前

    @俾路支我照你写的做了。在我的例子中,我添加了cookie信息,以便能够重用会话。

       // get the cookie if need
        String cookies = conn.getHeaderField("Set-Cookie");
    
        // open the new connnection again
        conn = (HttpURLConnection) new URL(newUrl).openConnection();
        conn.setRequestProperty("Cookie", cookies);
    
    推荐文章