Getting the Redirected URL from the Original URL

前端 未结 9 1494
终归单人心
终归单人心 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

提交回复
热议问题