我在一个名为myUrl的NSString中有一个url。当我NSLog myUrl时,我看到以下内容:
http://dl.dropbox.com/u/57665723%2FChocolatesRUs%2FJellies%2FUn-sugared%2Fgen%20no%20sugar.pdf
然后尝试使用此URL进行连接,如下所示:
NSURL* url = [ NSURL URLWithString:nextFileURL ];
NSMutableURLRequest* request = [ [ NSMutableURLRequest alloc ] initWithURL: url ];
NSURLConnection* conn = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];
我收到以下错误:
errorcode=-1000, error=Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x27e540 {NSUnderlyingError=0x26d800 "bad URL", NSLocalizedDescription=bad URL}
我尝试过使用
NSString* myUrl = [myUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
但这也不起作用。
有人能看到问题吗?
答案 0 :(得分:1)
也许您可以使用stringByAddingPercentEscapesUsingEncoding
代替stringByReplacingPercentEscapesUsingEncoding
来转义您的网址。
答案 1 :(得分:1)
stringByAddingPercentEscapesUsingEncoding删除空格并添加百分数
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]];