Getting the Redirected URL from the Original URL

前端 未结 9 1477
终归单人心
终归单人心 2020-12-05 14:57

I have a table in my database which contains the URLs of some websites. I have to open those URLs and verify some links on those pages. The problem is that some URLs get red

相关标签:
9条回答
  • 2020-12-05 15:01

    This code works for me

    var request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = "POST";
    request.AllowAutoRedirect = true;
    request.ContentType = "application/x-www-form-urlencoded";
    var response = request.GetResponse();
    

    //After sending the request and the request is expected to redirect to some page of your website, The response.ResponseUri.AbsoluteUri contains that url including the query strings //(www.yourwebsite.com/returnulr?r=""... and so on)

    Redirect(response.ResponseUri.AbsoluteUri); //then just do your own redirect.
    

    Hope this helps

    0 讨论(0)
  • 2020-12-05 15:01
    string url = ".......";
    var request = (HttpWebRequest)WebRequest.Create(url);
    var response = (HttpWebResponse)request.GetResponse();
    
    string redirectUrl = response.ResponseUri.ToString();
    
    0 讨论(0)
  • 2020-12-05 15:11

    You could check the Request.UrlReferrer.AbsoluteUri to see where i came from. If that doesn't work can you pass the old url as a query string parameter?

    0 讨论(0)
  • 2020-12-05 15:14

    Async HttpClient versions:

    // works in .Net Framework and .Net Core
    public static async Task<Uri> GetRedirectedUrlAsync(Uri uri, CancellationToken cancellationToken = default)
    {
        using var client = new HttpClient(new HttpClientHandler
        {
            AllowAutoRedirect = false,
        }, true);
        using var response = await client.GetAsync(uri, cancellationToken);
    
        return new Uri(response.Headers.GetValues("Location").First();
    }
    
    // works in .Net Core
    public static async Task<Uri> GetRedirectedUrlAsync(Uri uri, CancellationToken cancellationToken = default)
    {
        using var client = new HttpClient();
        using var response = await client.GetAsync(uri, cancellationToken);
    
        return response.RequestMessage.RequestUri;
    }
    

    P.S. handler.MaxAutomaticRedirections = 1 can be used if you need to limit the number of attempts.

    0 讨论(0)
  • 2020-12-05 15:18

    use this code to get redirecting url

    public void GrtUrl(string url)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.AllowAutoRedirect = false;  // IMPORTANT
    
            webRequest.Timeout = 10000;           // timeout 10s
            webRequest.Method = "HEAD";
            // Get the response ...
            HttpWebResponse webResponse;
            using (webResponse = (HttpWebResponse)webRequest.GetResponse())
            {
                // Now look to see if it's a redirect
                if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
                {
                    string uriString = webResponse.Headers["Location"];
                    Console.WriteLine("Redirect to " + uriString ?? "NULL");
                    webResponse.Close(); // don't forget to close it - or bad things happen!
                }
    
            }
    
        }
    
    0 讨论(0)
  • 2020-12-05 15:21

    The URL you mentioned uses a JavaScript redirect, which will only redirect a browser. So there's no easy way to detect the redirect.

    For proper (HTTP Status Code and Location:) redirects, you might want to remove

    req.AllowAutoRedirect = false;
    

    and get the final URL using

    myResp.ResponseUri
    

    as there can be more than one redirect.

    UPDATE: More clarification regarding redirects:

    There's more than one way to redirect a browser to another URL.

    The first way is to use a 3xx HTTP status code, and the Location: header. This is the way the gods intended HTTP redirects to work, and is also known as "the one true way." This method will work on all browsers and crawlers.

    And then there are the devil's ways. These include meta refresh, the Refresh: header, and JavaScript. Although these methods work in most browsers, they are definitely not guaranteed to work, and occasionally result in strange behavior (aka. breaking the back button).

    Most web crawlers, including the Googlebot, ignore these redirection methods, and so should you. If you absolutely have to detect all redirects, then you would have to parse the HTML for META tags, look for Refresh: headers in the response, and evaluate Javascript. Good luck with the last one.

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