Unable to create Request: Bad URL - objective-c

When i try to look for a location like this: Florida,USA it goes OK, but when i try it like this: Florida USA, i got that error:
Unable to create Request (bad url?)
The problem belongs with the spaces on the location taped, is there any way to resolve that?
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",theLocationString];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];

I think the problem is that you need to urlencode your string. A space character isn't a valid url string:
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",theLocationString];
//Create a URL object.
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
If NSASCIIStringEncoding doesn't work you could try: NSUTF8StringEncoding
..fredrik

Related

Issues with escaping a NSString

I want to escape an NSString to use in NSURL. Here the line...
NSURL *url = [NSURL URLWithString:#"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg" stringByAddingPercentEscapesUsingEncoding:NSUFT8StringEncoding];
But I get an error:
No known class method for selector: "URLWithString:string By Adding
Percent Escape Using Encodin"
Could you figured out what's the problem?
You mean surely:
NSURL *url = [NSURL URLWithString:[#"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
And take care of NSUFT8StringEncoding vs NSUTF8StringEncoding :)

NSURLRequest with url containing special characters

I'm having the same problem as:
NSURL with special characters
But tried their solution. Can't get my NSURLRequest to work with åöä characters. If the variable "string" contains åöä the request return null. Also tried with NSISOLatin1StringEncoding.
NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://suggestqueries.google.com/complete/search?output=firefox&q=%#", encodedString];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
This works:
http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%B6%C3%A4 (åöä)
Any ideas?
EDIT:
Using the debugger the NSURL looks correct:
string __NSCFString * #"åäö" 0x0a77cd60
encodedString __NSCFString * #"%C3%A5%C3%A4%C3%B6" 0x0a77fc40
url NSURL * #"http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0
Solved: The problem was not the NSURL it was how the return NSDATA was interpreted.
Instead of using
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
try using
NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];

Contents from url to string

NSString *urlAddress = [NSString stringWithFormat:#"http://www.domain.com?input=%#",[alertView textFieldAtIndex:0].text];
NSURL *myUrl = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *openURL = [NSString stringWithContentsOfURL:myUrl encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",openURL);
openUrl is always returning (null), probably because of the encoding, but I don't know how to fix it.
Double check these three things:
Check that the URL which you are pointing to is a valid URL. The null value could be a result of an invalid URL.
Catch the NSError which the stringWithContentsOfURL throws. That will give you some insight on what is wrong.
Like you suspect, try changing the string encoding.
NSError *error;
NSString *urlString = [NSString stringWithFormat:#"http://www.domain.com?input=%#", stringVariable];
NSURL *urlAdress = [NSURL URLWithString:urlString];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:urlAdress encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", urlContent);
This works fine, the problem was an failing internet connection.

How to encode sharp sign in NSURL

I have this scenario that I need to send a GET http request to the remote server. I went with NSURLConnection and intercept the request in HTTPScoop.
The url format is something like this:
http://domain.com?key=username##somehash&url=someotherurl.com
I am doing it like this:
NSString *urlString = [[NSString alloc]init];
urlString = #"http://domain.com?key=username##somehash&url=someotherurl.com";
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:encodedString]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
In this case I didn't escape the # sign, and the request I see in httpscoop is:
http://domain.com?key=username223somehash&url=someotherurl.com
If I escape the sharp sign to %23, it gets to something like this in httpscoop:
http://domain.com?key=username22523somehash&url=someotherurl.com
I have tried different combinations but always have issue with the sharp sign. Are there any walk-around for this? Thanks!
replace # with %23 (source).
edit - oops, didn't see the rest of your message. Not sure about NSURL, but I have had difficulty encoding parameters in URLs with NSURL before, too. I ended up using ASIHTTPRequest, which took care of all the encoding issues. I would recommend doing the same.

extract data from cocoa http get request

I implemented the following code:
NSURL *url = [ NSURL URLWithString:[ NSString stringWithFormat: #"http://www.google.com/search?q=%#", query ] ];
NSURLRequest *request = [ NSURLRequest requestWithURL: url ];
I want to extract the body from what I receive back from the url above. I attempted:
NSData *data = [request HTTPBody];
The data variable doesn't return any data? Am I going about extracting the data out of the request the right way?
Thanks!
If you're just trying to get a web page, you can use this.
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: #"http://www.google.com"] ];
NSString *test = [NSString stringWithContentsOfURL:url];
If you really want to convert the data from NSData you can use this:
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSURLRequest just defines a request — it doesn't do anything by itself. To actually make a request, you need to give the request to an NSURLConnection.
Also, as indicated in the documentation, the HTTPBody is data that's sent with the request, not the response body.
There is an article on www.eigo.co.uk which shows exactly how to do the request and get the response in a string variable but the chunk of code you need is...
NSString * strResult = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
Check out the article here http://www.eigo.co.uk/iPhone-Submitting-HTTP-Requests.aspx