检查URL中的特定网页的可达性 [英] checking reachability against a specific page in a URL

查看:340
本文介绍了检查URL中的特定网页的可达性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我阅读了这个问题我发现使用 reachabilityWithHostName 不能使用这样的URL: mySite.com/service.asmx ,是否还有使用 reachabilityWithHostName 或任何可达性类方法检查此URL的可达性?



感谢这么多。

解决方案

-reachabilityWithHostname:设计为快速,故障快速的机制,以确定是否具有到主机的基本网络连接。如果您需要验证是否可以下载特定的网址,您需要使用 NSURLConnection 来检索网址的内容,以验证其是否真正可用



根据您是否需要在前台或后台执行此操作,您可以使用简单阻止:

  +(NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error 

或者您可以使用更复杂的方法创建NSURLConnection对象,设置一个代理接收响应并等待这些响应



对于简单情况:

  NSURL * myURL = [NSURL URLWithString:@http://example.com/service.asmx]; 
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:@HEAD];
NSURLResponse * response;
NSError * error;
NSData * myData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& error];

如果您收到一个非nil的myData,你有一些连接。 响应错误将告诉您服务器对您的响应(在响应的情况下, -nil myData)或在nil myData的情况下发生什么类型的错误。



对于非平凡的情况,你可以从Apple的使用NSURLConnection



如果你不想停止你的前台进程,你可以做这两种不同的方法。上述文档将提供有关如何实现代理等的信息。然而,更简单的实现方式是使用GCD在后台线程上发送同步请求,然后在完成后在主线程上发送消息。 p>

这样的东西:

  NSURL * myURL = [NSURL URLWithString:@ http://example.com/service.asmx]; 
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:@HEAD];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,NULL),^ {
NSURLResponse * response;
NSError * error;
NSData * myData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error :& error];
BOOL reachable;

if(myData){
//我们可能到达,检查响应
reachable = YES;
} else {
//我们可能无法访问,检查错误:
reachable = NO;
}

//现在调用自己的主线程
dispatch_async(dispatch_get_main_queue(),^ {
[self setReachability:reachable];
});
});


After I have read the answer for this question I have found that using reachabilityWithHostName does not work with a URL such as this one: mySite.com/service.asmx , is there anyway to check reachability against this URL using reachabilityWithHostName or any reachability class method ?

thanks so much in advance.

解决方案

The Reachability class and -reachabilityWithHostname: is designed to be a quick, fail-fast mechanism to determine whether you have basic network connectivity to the host. If you need to verify that a particular URL can be downloaded, you need to be looking at using NSURLConnection to retrieve the contents of the URL in order to verify that it is truly available.

Depending on whether you need to do this in the foreground or background, you can either use the simple-but-blocking:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

or you can use the more complicated method of creating an NSURLConnection object, setting up a delegate to receive responses and wait for those responses to come in.

For the simple case:

 NSURL *myURL = [NSURL URLWithString: @"http://example.com/service.asmx"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
 [request setHTTPMethod: @"HEAD"];
 NSURLResponse *response;
 NSError *error;
 NSData *myData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

If you receive back a non-nil myData, you've got some kind of connectivity. response and error will tell you what the server responded to you (in the case of response and if you received a non-nil myData) or what kind of error occurred, in the case of a nil myData.

For the non-trivial case, you can get good guidance from Apple's Using NSURLConnection.

If you don't want to stall your foreground process, you can do this two different ways. The above documentation will provide information on how to implement the delegate, etc. However, a simpler implementation would be to use GCD to send the Synchronous request on a background thread, and then message yourself on the main thread when you are done.

Something like this:

 NSURL *myURL = [NSURL URLWithString: @"http://example.com/service.asmx"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
 [request setHTTPMethod: @"HEAD"];
 dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^{
      NSURLResponse *response;
      NSError *error;
      NSData *myData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
      BOOL reachable;

      if (myData) {
            // we are probably reachable, check the response
            reachable=YES;
      } else {
            // we are probably not reachable, check the error:
            reachable=NO;
      }

      // now call ourselves back on the main thread
      dispatch_async( dispatch_get_main_queue(), ^{
            [self setReachability: reachable];
      });
 });

这篇关于检查URL中的特定网页的可达性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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