检查 URL 文件是否存在

发布于 2024-11-26 15:46:07 字数 756 浏览 0 评论 0原文

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

我有大约 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”是对象的名称。这个方法正在做我想要的事情,但问题是,使用 NSData 它首先“下载”文件,当 URL 被验证为文件时,电影会在电影播放器​​中再次加载。这意味着加载文件需要两倍的时间。

建议?

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:

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.

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏闹i 2024-12-03 15:46:07

我花了一段时间才找到有关此主题的先前问题之一的答案< /a>.引用:

您可以使用 NSMutableURLRequest 发送 HTTP HEAD 请求
(有一个名为 setHTTPMethod 的方法)。你会得到同样的
响应标头与 GET 一样,但您不必下载整个
资源体。如果你想同步获取数据,请使用
sendSynchronousRequest… 方法NSURLConnection

这样您就可以知道该文件是否存在,如果存在则不会全部下载。

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

You can use a NSMutableURLRequest to 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.

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

我很坚强 2024-12-03 15:46:07

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

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

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

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

-(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 
    }
} 

还可以找到各种状态码此处

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

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

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

花桑 2024-12-03 15:46:07
NSError *err;
if ([videoURL checkResourceIsReachableAndReturnError:&err] == NO)
   NSLog(@"wops!");
NSError *err;
if ([videoURL checkResourceIsReachableAndReturnError:&err] == NO)
   NSLog(@"wops!");
紫竹語嫣☆ 2024-12-03 15:46:07

以下是已接受答案的代码(为了您的方便):

如何拨打电话

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

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];
王权女流氓 2024-12-03 15:46:07

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

请查看此处了解如何执行此操作。

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

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.

Check here on how to do this.

You could of course also do this by using a NSURLRequest with 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文