开发者

Check if file on website exists

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 directory. However, if the file does not exists, Apache presen开发者_开发问答ts a HTML error page (which I download instead of test.licence). Checking the file size is a fuzzy solution, since it varies.

So, how do I check, if a online file exists (not more)? I'm looking for a simple yet clean solution.

Regards,

Stefan


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).


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜