I got the following error when trying to connect to my web service using NSURL:
Failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL"
My URL was a GET
request and so had several parameters in it. At first, I tried to URL encode the spaces manually using %20
(you’ll have to use %%
in your literal string to escape the %
sign), which unfortunately did not work. The real fix was simple, however, just use the stringByAddingPercentEscapesUsingEncoding
method of the NSString
object, e.g.:
NSString *myURLString = [NSString stringWithFormat:@"https://www.dforge.net/api/v1/the-webservice?p=a parameter with spaces"];
NSURL *url = [NSURL URLWithString: [myURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"GET"];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
In my case, I used UTF-8 encoding.