java, android, resolve an url, get redirected uri

后端 未结 2 940
故里飘歌
故里飘歌 2020-12-14 22:35

I have an authentication protected url : www.domain.com/alias

that when requested will return another url: www.another.com/resource.mp4 (not protected)

I wou

相关标签:
2条回答
  • 2020-12-14 22:46

    This is a problem i used to have concerning URL redirects. Try the following code:

    URL url = new URL(url);
    HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
    ucon.setInstanceFollowRedirects(false);
    URL secondURL = new URL(ucon.getHeaderField("Location"));
    URLConnection conn = secondURL.openConnection();
    

    The "magic" here happens in these 2 steps:

    ucon.setInstanceFollowRedirects(false);
    URL secondURL = new URL(ucon.getHeaderField("Location"));
    

    By default InstanceFollowRedirects are set to true, but you want to set it to false to capture the second URL. To be able to get that second URL from the first URL, you need to get the header field called "Location".

    0 讨论(0)
  • 2020-12-14 23:04

    I have eliminated this issue on sites where we have a MikroTik router by using a Layer 7 protocol filter as shown below. This doesn't help the devices off the WiFi network (obviously) but at least gives them some reprieve when they are connected to home and/or work WiFi networks.

    Firstly, create the protocol definition:

    /ip firewall layer7-protocol
    add comment="Frigging javascript redirects on chrome browsers" \
        name=Javascript_Redirect \
        regexp="^.+(spaces.slimspot.com|mostawesomeoffers.com).*\$"
    

    Now, to actually filter this traffic out

    /ip firewall filter
    add action=drop chain=forward comment=\
        "Block and log Javascript_Redirect L7 Protocol" layer7-protocol=\
        Javascript_Redirect log=yes log-prefix=JSredirect_
    

    Other firewalls that have Layer 7 filtering capacity could also block these redirects in a similar way.

    0 讨论(0)
提交回复
热议问题