iOS download file from a URL that will redirect to

2019-09-01 00:55发布

问题:

I've been trying for 2 days and cant figure this out.

Basically I have a URL and I try to download the file that associate with it using ASIHTTPRequest, but the URL I have is not the link of the file itself, but that URL will redirect to the link of the actual file. I try to give that link to ASIHTTPRequest for download, but it didn't. SO I think I have to somehow get the redirected url and feed that to ASIHTTPRequest to download the file.

How can I do this?

Thanks all for reading :)

回答1:

You implement this delegate method of ASIHTTPRequest - (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL the Method is called, but after the request isn't redirected as I asumed. You have to redirect the request by yourself:

  - (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL 
  {
   // modify request here, e.g. set request headers again...
   [request redirectToURL:newURL];
  }