java urlconnection get the final redirected URL

后端 未结 7 1826
死守一世寂寞
死守一世寂寞 2020-11-30 13:07

I have a url which redirects to another url.I want to be able to get the final redirected URL.My code:

    public class testURLConnection
    {
    public st         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 13:24

    My first idea would be setting instanceFollowRedirects to false, or using URLConnection instead.

    In both cases, the redirect won't be executed, so you will receive a reply to your original request. Get the HTTP Status value and, if it is 3xx, get the new redirect value.

    Of course there may be a chain of redirects, so probably you will want to iterate until you reach the real (status 2xx) page.

提交回复
热议问题