Send http request with Chinese words - iphone

NSString *urlAddress = #"http://www.test.com/test.jsp?Query=哈囉";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
NSURLResponse* myURLResponse;
NSError* myError;
NSData* data = [NSURLConnection sendSynchronousRequest: requestObj returningResponse:&myURLResponse error:& myErr];
I want to use http get method to send request, but failed.
English alphabets work well.
I.e., I want to convert "哈囉" to "%E5%93%88%E5%9B%89" so that the response will successfully return. It seems not to be converted by itself automatically.
Anyone could help me solve this problem?
Thanks a lot!

urlAddress = [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
(docs)

Related

Can't fetch data from NSURL

I am fetching result from google place search url, if I search for "Newyork", everything works fine. But if I give "New york", the result json is different and I can't take it in NSData.
This is the url I am using:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=New york&sensor=true&key=MY_KEY
I am using below code to fetch the data:
NSURL *googleURL = [NSURL URLWithString:url]; //url is above url
NSURLResponse *response;
NSData *data;
NSError *error;
data = [NSURLConnection sendSynchronousRequest: [NSURLRequest
requestWithURL: googleURL] returningResponse: &response error: &error];
NSLog(#"data %#",data); // but I am getting null here
How to overcome this? Thanks in advance
Assuming that url is:
NSString *url = #"https://maps.googleapis.com/maps/api/place/textsearch/json?query=New york&sensor=true&key=MY_KEY";
Try using this:
NSURL *googleURL = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
This will format the string for URLs containing spaces. To confirm it is working, print it out to the console.
NSLog(#"URL with Percent Escapes: %#", [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
It should return something like this (I believe the percent escape for a space is %20).
URL with Percent Escapes: https://maps.googleapis.com/maps/api/place/textsearch/json?query=New%20york&sensor=true&key=MY_KEY

ASIHTTPRequest setPostValue iPhone

I'm accessing a PHP script on a server.
The request URL is like this:
example.com/cgi-bin/getEvent.cgi?EID=19573
When I put in the request via a browser, I get back my expected results.
However when I use the ASIHTTP Form request, I'm getting back a result
like the EID isn't being passed via HTTP.
NSString *eventID = #"19573";
NSString * const EVENT_URL = #"http://example.com/cgi-bin/getEvent.cgi";
-(void)callWebservice
{
NSURL *url = [NSURL URLWithString:EVENT_URL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:eventID forKey:#"EID"];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDelegate:self];
[request startAsynchronous];
}
Anyone know of a method to see the full URL being requested?
Or have any clue why this wouldn't be working?
Thanks in advance.
My guess is that your PHP script is expecting the parameter to be sent on the query string (i.e. as a GET request) rather than as a POST parameter.
If that's the case, you can fix it be sending your request as follows:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#?EID=%#",EVENT_URL,eventID]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

iPhone & Google Places API

I am using Google place API in my application. Well, its working fine and giving response for some places. But for some places, its giving "ZERO RESULTS".
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%#&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect= [[NSURLConnection alloc]initWithRequest:request delegate:self];
Can anyone give me an idea where I am getting wrong?
If the location name is not valid it will give you zero results & in some cases because of extra spaces in the string you will get URL as null. So update your code as I mentioned below and check it out.
NSString *googleApi = [NSString stringWithFormat:#"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%#&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text];
NSString *finalURL = [googleApi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:finalURL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect= [[NSURLConnection alloc]initWithRequest:request delegate:self];
It may work for you.

How can I set the 30 sec timeout? NSURL

How can I set the 30 sec timeout? NSURL
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
From this previous SO question timeout stringwithcontentsofurl
In this case, you might be best off using the NSURLConnection class method +sendSynchronousRequest:returningResponse:error:; it'll return a data object you can initialize your string with (using -initWithData:encoding:). You can specify a timeout by creating an NSURLRequest using its -initWithURL:cachePolicy:timeoutInterval: method, then pass that request as the first parameter of +sendSynchronousRequest:returningResponse:error:.
When you create an NSURLRequest from your NSURL, use requestWithURL:cachePolicy:timeoutInterval: to specify a timeout interval in seconds. For instance:
NSURLRequest* request = [NSURLRequest requestWithURL: myUrl
cachePolicy: NSURLRequestReloadIgnoringCacheData
timeoutInterval:30.0];

Get content of webservice

I've got an URL like here. When I type that into Safari's address bar I see an result like "Error" or "OK".
So, how do I properly call that URL from within my code and get that result as a string?
I've tried it with NSURLConnection, NSURLRequest and NSURLResponse but my response object is always nil.
The "response" in those classes refers to the protocol response (HTTP headers, etc.), not the content.
To get the content, you have a few options:
Use NSURLConnection in asynchronous mode: Using NSURLConnection
Use NSURLConnection in synchronous mode:
// Error checks omitted
NSURL *URL = [NSURL URLwithString:#"http://www.myserver.com/myservice.php?param=foobar"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:nil];
Use [NSString stringWithContentsOfURL:]
NSURL *URL = [NSURL URLwithString:#"http://www.myserver.com/myservice.php?param=foobar"];
NSString *content = [NSString stringWithContentsOfURL:URL];
Of course, you should use options 2 and 3 only if your content will be really small in size, to maintain responsiveness.