0

I have a url in an NSString called myUrl. When I NSLog myUrl I see the following:

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

I then try connecting using this url as follows:

NSURL* url = [ NSURL URLWithString:nextFileURL ];
NSMutableURLRequest* request =  [ [ NSMutableURLRequest alloc ] initWithURL: url ];
NSURLConnection* conn = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];

I am getting the following error:

errorcode=-1000, error=Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x27e540 {NSUnderlyingError=0x26d800 "bad URL", NSLocalizedDescription=bad URL}

I have tried using

NSString* myUrl = [myUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

but this doesn't work either.

Can anyone see the problem?

whatdoesitallmean
  • 1,586
  • 3
  • 18
  • 40

2 Answers2

1

Maybe you can escape your url with stringByAddingPercentEscapesUsingEncoding instead of stringByReplacingPercentEscapesUsingEncoding.

fredrik
  • 13,282
  • 4
  • 35
  • 52
alexandresoli
  • 918
  • 1
  • 9
  • 18
1

stringByAddingPercentEscapesUsingEncoding is remove spaces and adding percents

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]];
Mohamed Jaleel Nazir
  • 5,776
  • 3
  • 34
  • 48