[英]iOS, check if an http or https server is listening in a specific port
我需要查找Web服务器(有时在http中,有时在https中)是否在侦听。 我希望同步进行,并且超时时间较小,例如2-3秒。
正确的做法是什么? 我需要向后兼容iOS 5。
更新:我需要在没有外部库的情况下进行操作。
由于OP无法使用外部库,因此请实现NSURLConnectionDelegate
并使用:
- (void)testUrl:(NSString *)url
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:3.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int responseStatusCode = [httpResponse statusCode];
NSLog(@"GOT STATUS CODE: %d", responseStatusCode);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Done!");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.