I'd like to submit to the following URL in a Cocoa app and save the results to a string (although XML is probably better):
http://translate.google.com/translate_t#en|fr|hi%20there%20all
The page keeps returning an error. Here's my code:
NSString *urlString = @"http://translate.google.com/translate_t#en|fr|hi%20there%20all";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSString *content = [[NSString alloc] initWithBytes:[urlData bytes]
The above causes the URL to turn into:
http://translate.google.com/translate_t%23en%7Cfr%7Chi%2520there%2520all
Which results in a 404 from Google. If I try the URL without escaping, I get zero bytes returned. Any ideas on how to make this work?
--- EDIT ---
The source of the problem is that the value of url is nil. Remove the EscapesUsingEncoding line (line 2) and check the value of url. I've the pipes in the URL are causing [NSURL URLWithString:urlString]; to return nil. Escaping the pipes with their hex values, 7C (http://translate.google.com/translate%5Ft#en%7cfr%7chi%20there%20all), returns data. But then I get zero bytes again on the NSString *content line.