Download file through code that has a redirect?

前端 未结 2 601
太阳男子
太阳男子 2020-12-11 22:59

I have some urls in the database. The problem is there urls are urls that redirect to what I want.

I have something like this

http://www.mytestsite.com/test/

相关标签:
2条回答
  • 2020-12-11 23:23

    I think you are after the HttpWebRequest.AllowAutoRedirect Property. The property gets or sets a value that indicates whether the request should follow redirection responses.

    Example taken from MSDN

    HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");    
    myHttpWebRequest.MaximumAutomaticRedirections=1;
    myHttpWebRequest.AllowAutoRedirect=true;
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
    
    0 讨论(0)
  • 2020-12-11 23:28

    I had issues trying to get HttpWebRequest to always fully redirect when using it with SharePoint external URLs; I simply couldn't get it to work.

    After a lot of faffing about I discovered that this can be done with WebClient too and that proved more reliable for me.

    To get that to work with WebClient you seem to have to create a class that derives from WebClient so that you can manually force AllowAutoRedirect to true.

    I wrote up a bit more on this in this answer, which borrows its code from this question.

    The key code was:

    class CustomWebclient: WebClient
    {
      [System.Security.SecuritySafeCritical]
      public CustomWebclient(): base()
     {
     }
     public CookieContainer cookieContainer = new CookieContainer();
    
    
     protected override WebRequest GetWebRequest(Uri myAddress)
     {
           WebRequest request = base.GetWebRequest(myAddress);
           if (request is HttpWebRequest)
          {
               (request as HttpWebRequest).CookieContainer =   cookieContainer;
               (request as HttpWebRequest).AllowAutoRedirect = true;
          }
          return request;
      }
    }
    
    0 讨论(0)
提交回复
热议问题