CFNetwork Error : The connection failed due to a malformed URL

 ̄綄美尐妖づ 提交于 2019-12-11 09:36:51

问题


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

The connection failed due to a malformed URL

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

  • When and Why this error is triggered ?
  • How to get rid from this error?

回答1:


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;
}


来源:https://stackoverflow.com/questions/12383602/cfnetwork-error-the-connection-failed-due-to-a-malformed-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!