如何简单地测试到IP的本地Wifi连接,例如192.168.0.100的代码状态?(How to simply test a local Wifi Connection to an IP, for e

编程入门 行业动态 更新时间:2024-10-27 10:21:23
如何简单地测试到IP的本地Wifi连接,例如192.168.0.100的代码状态?(How to simply test a local Wifi Connection to an IP, for example 192.168.0.100 with code status?)

你知道我是否可以简单地测试是否有WIFI本地连接? 例如,如果可以访问URL 192.168.0.100。 我尝试了Reachability没有成功。 它告诉我它是连接的,但事实并非如此。

我想首先测试是否存在本地WIFI连接,然后当我确定存在连接时,启动该Web服务:

- (void)callWebService:(NSString *)url withBytes:(NSString *) bytes //GET { NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init]; NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [request setURL:[NSURL URLWithString:[url stringByAppendingString: url_string]]]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setTimeoutInterval:timeOut]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; //try NSURLSession [connection start]; }

提前致谢。

Do you know if I can simply test if there is a WIFI local connection ? For example if the url 192.168.0.100 is reachable. I tried with the Reachability without success. It tells me that it is connected, but it is not true.

I would like first to test if there is a local WIFI connection, and then when I am sure there is a connection, launch that Web Service :

- (void)callWebService:(NSString *)url withBytes:(NSString *) bytes //GET { NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init]; NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [request setURL:[NSURL URLWithString:[url stringByAppendingString: url_string]]]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setTimeoutInterval:timeOut]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; //try NSURLSession [connection start]; }

Thanks in advance.

最满意答案

NSURLConection有许多委托方法。 尝试以下之一:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self.download_connection cancel]; // optional depend on what you want to achieve. self.download_connection = nil; // optional DDLogVerbose(@"Connection Failed with error: %@", [error description]); } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; NSInteger state = [httpResponse statusCode]; if (state >= 400 && state < 600) { // something wrong happen. [self.download_connection cancel]; // optional self.download_connection = nil; // optional } }

NSURLConection have many delegate methods. try one of the following:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self.download_connection cancel]; // optional depend on what you want to achieve. self.download_connection = nil; // optional DDLogVerbose(@"Connection Failed with error: %@", [error description]); } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; NSInteger state = [httpResponse statusCode]; if (state >= 400 && state < 600) { // something wrong happen. [self.download_connection cancel]; // optional self.download_connection = nil; // optional } }

更多推荐

本文发布于:2023-07-21 09:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   代码   简单   测试   Wifi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!