0

有时启动 NSURLConnection 时出现这些错误。

但是,并不总是得到这个错误。

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x9eaa6b0 {NSUnderlyingError=0xae8c730 "bad URL", NSLocalizedDescription=bad URL}
4

1 回答 1

2

网址中有空格是该错误的原因。

NSString *urlString =[NSString stringWithFormat:@"&street=%@&street2=&city=%@&state=%@&
zipcode=%@&candidates=10", address.address2,address.city, address.state, address.zip5];
NSLog(@"BAD URL - %@",urlString ); // there is space in url

NSString *encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSLog(@" CORRECT URL - %@", encodedUrl); // url encode that space by %20


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL RLWithString:encodedUrl]];
于 2014-09-12T10:49:31.957 回答