2012-10-10 53 views
0

我有一個名爲myUrl的NSString中的網址。當我的NSLog myUrl我看到以下內容:iOS的錯誤的URL NSUrlConnection

http://dl.dropbox.com/u/57665723%2FChocolatesRUs%2FJellies%2FUn-sugared%2Fgen%20no%20sugar.pdf 

然後我嘗試使用這個網址如下連接:

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

bu這也不起作用。

任何人都可以看到問題嗎?

回答

1

也許你可以通過stringByAddingPercentEscapesUsingEncoding而不是stringByReplacingPercentEscapesUsingEncoding來逃避你的網址。

+0

這是固定的, - 非常感謝 – whatdoesitallmean

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]]; 
相關問題