CFNetwork错误:由于格式不正确的URL,连接失败 [英] CFNetwork Error : The connection failed due to a malformed URL

查看:267
本文介绍了CFNetwork错误:由于格式不正确的URL,连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行iphone应用程序时,我遇到以下错误

While running iphone app, several times i got the following Error

The connection failed due to a malformed URL

CFNetwork错误代码参考,我得到的信息此错误由CFNetwork触发

At CFNetwork Error Codes Reference, i got the info that this error triggered by CFNetwork


  • 何时和为什么会触发此错误?

  • 如何摆脱从这个错误?

推荐答案

NSURLConnection有一个方法canHandleRequest将返回一个布尔值,无论是url是否有效,例如

NSURLConnection has a method canHandleRequest which will return a boolean whether the url is valid or not, e.g.

NSString *strUrl = [NSString stringWithFormat:@"http://test.com/stopBoard/%i",busStopCode];
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
                                              cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                                          timeoutInterval:20.0];
// a cache policy of NO must be used otherwise the previous query is used if the phone is not online

BOOL canGo = [NSURLConnection canHandleRequest:request];
if(canGo){
    connection = [[NSURLConnection alloc] initWithRequest:request 
                                                 delegate:self 
                                         startImmediately:YES];


} else {
    self.errorMessage = @"\n\nurl check failed\n\n";
    return NO;
}

这篇关于CFNetwork错误:由于格式不正确的URL,连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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