Check if file on website exists

后端 未结 2 1156
执笔经年
执笔经年 2021-01-21 23:27

on the iPhone I want to check, if a specific file (test.licence) on a website exsists. My approach (so far) was to download the file and check if it exists in my Documents direc

相关标签:
2条回答
  • 2021-01-21 23:50

    Submit an NSURLRequest via NSURLConnection. When you receive a response, check whether the status code is 200 (it's there), 404 (it's not) or something else (a different thing happened).

    0 讨论(0)
  • 2021-01-21 23:50

    The 404 error code is the correct way to see if the page exists, but if you're intending on displaying the page in a UIWebView, you're going to want to check if the page exists first, and if it does, then you load it up again within the web view (if you did so first, you'd risk showing the Apache error page).

    In order to not use up bandwidth twice, for the first sanity check to see if the file exists, you can do a HEAD request, which grabs only the HTTP headers and not the body of the page. Simply add this to your code (assuming urlRequest is of type NSURLRequest):

    [urlRequest setHTTPMethod:@"HEAD"];
    
    0 讨论(0)
提交回复
热议问题