0

我在名为 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];

但这也不起作用。

任何人都可以看到问题吗?

4

2 回答 2

1

stringByAddingPercentEscapesUsingEncoding也许您可以使用而不是转义您的网址stringByReplacingPercentEscapesUsingEncoding

于 2012-10-10T14:42:44.490 回答
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]];
于 2014-11-15T05:39:57.050 回答