xcode 检查 URL 文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6856796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:35:15  来源:igfitidea点击:

Check if URL-file exist or not

iphonexcodeurlmpmovieplayercontroller

提问by Emil

I wonder how I can check if a file exist on a server or not, without downloading the data first.

我想知道如何在不先下载数据的情况下检查服务器上是否存在文件。

I have around 30 different objects and some of them is connected to a movie on a server. At the moment I use NSData to control if the the URL exist, and then shows the movie, or if it doesn't and then alerts the user that there is no video for that object. The code I use for the moment:

我有大约 30 个不同的对象,其中一些连接到服务器上的电影。目前我使用 NSData 来控制 URL 是否存在,然后显示电影,或者如果不存在,然后提醒用户该对象没有视频。我暂时使用的代码:

NSString *fPath = [[NSString alloc] initWithFormat:@"http://www.myserver/%@", [rows idNr]];
NSURL *videoURL = [NSURL URLWithString:fPath];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

url = [NSURL URLWithString:fPath];
[fPath release];

if (videoData) {
    [self performSelectorOnMainThread:@selector(playVideo:) withObject:url waitUntilDone:NO];
} else {
    NSLog(@"videodata false");
    errorLabel.hidden = NO;
    activityView.hidden = YES;
}

"rows idNr" is the name of the object. This method is doing what I want, but the problem is that with NSData it first "downloading" the file, and when the URL is validated as a file, the movie is loading once again in the movieplayer. This means that it takes twice as long to load the file.

“rows idNr”是对象的名称。这种方法正在做我想要的,但问题是使用 NSData 它首先“下载”文件,当 URL 被验证为文件时,电影将再次加载到电影播放器​​中。这意味着加载文件需要两倍的时间。

Suggestions?

建议?

回答by zoul

It took me a while to dig out my answer to one of the previous questions on this topic. Quote:

我花了一些时间才找到关于这个主题的先前问题之一的答案。引用:

You can use a NSMutableURLRequestto send a HTTP HEAD request (there's a method called setHTTPMethod). You'll get the same response headers as with GET, but you won't have to download the whole resource body. And if you want to get the data synchronously, use the sendSynchronousRequest…method of NSURLConnection.

您可以使用 aNSMutableURLRequest发送 HTTP HEAD 请求(有一种方法称为setHTTPMethod)。您将获得与 GET 相同的响应标头,但您不必下载整个资源主体。而如果要同步获取数据,则使用 的 sendSynchronousRequest…方法NSURLConnection

This way you'll know if the file exists and won't download it all if it does.

通过这种方式,您将知道文件是否存在,并且如果存在则不会全部下载。

回答by Anurag Soni

Make an URLConnecion object with desired url request and add NSURLConnectionDelegateinto .h file like I want to check "yoururl" is exist or not then you need to do is

使用所需的 url 请求创建一个 URLConnecion 对象并添加NSURLConnectionDelegate到 .h 文件中,就像我想检查“ yoururl”是否存在一样,那么您需要做的是

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://www.google.com"]];

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

and then you can track http status code in delegate function of NSURLConnectionDelegate

然后你可以在委托函数中跟踪http状态代码 NSURLConnectionDelegate

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    int code = [(NSHTTPURLResponse *)response statusCode];
    if (code == 404)
    {
        // website not found
        // do your stuff according to your need 
    }
} 

You can also find various status code here.

您还可以在此处找到各种状态代码。

回答by andreamazz

NSError *err;
if ([videoURL checkResourceIsReachableAndReturnError:&err] == NO)
   NSLog(@"wops!");

回答by Justin Oliver

Here's the code for the accepted answer (for your convenience):

这是已接受答案的代码(为方便起见):

How to make call

如何拨打电话

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"HEAD"];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

回答by Man of One Way

You could do this by checking the size of the file via an FTP server, using the SIZE command. If the file size is zero then the file simply do not exist.

您可以使用 SIZE 命令通过 FTP 服务器检查文件的大小来完成此操作。如果文件大小为零,则该文件根本不存在。

Check hereon how to do this.

点击这里了解如何做到这一点。

You could of course also do this by using a NSURLRequestwith NSURLConnection, checking for the status to be either 200 (success) or 404 (failed). The 404 status doesn't have to be that the file doesn't exist though, it could also be that the file just couldn't be retrieved.

您当然也可以通过使用NSURLRequestwith来执行此操作NSURLConnection,检查状态是否为 200(成功)或 404(失败)。404 状态不一定是文件不存在,也可能是文件无法检索。