查找URL是否有效并且是否存在? [英] Finding whether a URL is valid and does it exists?

查看:81
本文介绍了查找URL是否有效并且是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道网址即时提供是否有效,以及网上是否存在..

I want to find that whether the URL im giving is valid or not, and whether it exists in internet or not..

有没有办法做到这一点使用objective-C?

Is there any way to do that using objective-C?

推荐答案

通过执行以下操作,您可以查看URL是否有效:

You can see if a URL is valid or not by doing the following:

NSString *urlString = ... // some URL to check
NSURL *url = [NSURL URLWithString:urlString];
if (url) {
    // valid URL (meaning it is the proper format)
}

要查看Internet中是否存在URL,您需要执行HEAD请求并检查结果。这比加载URL的所有数据更有效。

To see if the URL exists in the Internet, you need to perform a HEAD request and check the result. This is more efficient than loading all of the data for the URL.

NSMutableURLRequest request = [NSMutableURLRequest requestWithURL:inURL];
[request setHTTPMethod:@"HEAD"];

NSURLConnection connection = [NSURLConnection connectionWithRequest:request delegate:self];


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    if ([(NSHTTPURLResponse *)response statusCode] == 200) {
        // url exists
    }
}

这篇关于查找URL是否有效并且是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆