Using WebClient in C# is there a way to get the URL of a site after being redirected?

后端 未结 8 1473
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 03:21

Using the WebClient class I can get the title of a website easily enough:

WebClient x = new WebClient();    
string source = x.DownloadString(s);
string titl         


        
相关标签:
8条回答
  • 2020-11-29 03:45

    The WebClient class has an option to follow redirects. Set that option and you should be fine.

    0 讨论(0)
  • 2020-11-29 03:51

    I got the Uri for the redirected page and the page contents.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
    request.AllowAutoRedirect = true;
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    
    strLastRedirect = response.ResponseUri.ToString();
    
    StreamReader reader = new StreamReader(dataStream);              
    string strResponse = reader.ReadToEnd();
    
    response.Close();
    
    0 讨论(0)
  • 2020-11-29 03:53

    I know this is already an answered question, but this works pretty to me:

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://tinyurl.com/dbysxp");
     request.AllowAutoRedirect = false;
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     string redirUrl = response.Headers["Location"];
     response.Close();
    
     //Show the redirected url
     MessageBox.Show("You're being redirected to: "+redirUrl);
    

    Cheers.! ;)

    0 讨论(0)
  • 2020-11-29 03:53

    With an HttpWebRequest, you would set the AllowAutoRedirect property to false. When this happens, any response with a status code between 300-399 will not be automatically redirected.

    You can then get the new url from the response headers and then create a new HttpWebRequest instance to the new url.

    With the WebClient class, I doubt you can change it out-of-the-box so that it does not allow redirects. What you could do is derive a class from the WebClient class and then override the GetWebRequest and the GetWebResponse methods to alter the WebRequest/WebResponse instances that the base implementation returns; if it is an HttpWebRequest, then set the AllowAutoRedirect property to false. On the response, if the status code is in the range of 300-399, then issue a new request.

    However, I don't know that you can issue a new request from within the GetWebRequest/GetWebResponse methods, so it might be better to just have a loop that executes with HttpWebRequest/HttpWebResponse until all the redirects are followed.

    0 讨论(0)
  • 2020-11-29 03:57

    HttpWebRequest.AllowAutoRedirect can be set to false. Then you'd have to manually http status codes in the 300 range.

    // Create a new HttpWebRequest Object to the mentioned URL.
    HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");    
    myHttpWebRequest.MaximumAutomaticRedirections=1;
    myHttpWebRequest.AllowAutoRedirect=true;
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();  
    
    0 讨论(0)
  • 2020-11-29 04:02

    If I understand the question, it's much easier than people are saying - if you want to let WebClient do all the nuts and bolts of the request (including the redirection), but then get the actual response URI at the end, you can subclass WebClient like this:

    class MyWebClient : WebClient
    {
        Uri _responseUri;
    
        public Uri ResponseUri
        {
            get { return _responseUri; }
        }
    
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            _responseUri = response.ResponseUri;
            return response;
        }
    }
    

    Just use MyWebClient everywhere you would have used WebClient. After you've made whatever WebClient call you needed to do, then you can just use ResponseUri to get the actual redirected URI. You'd need to add a similar override for GetWebResponse(WebRequest request, IAsyncResult result) too, if you were using the async stuff.

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