Sending a request and receiving a response

I have php code running on my server that I call in my web service. It processes the data in integer values. Send.How can I get this? Here is my request url:

NSString *requestURL=[NSString stringWithFormat:@"%@?u=%@& p=%@&platform=ios",url,txtUserName.text,txtPassword.text]; 

Update comment: I have a php file on my server. It takes 3 arguments and registers my users and returns the value as 1 (for success, 2 for duplicates). I need to send a request to my server as:

 url="http://smwebtech.com/Pandit/web_service/signup.php?u=Test&p=password&platform=ios" 

How can I send this request to the server and get the return value from the server?

+1
ios objective-c
source share
3 answers

You can use NSURLConnection. You must use the implementation of the NSURLConnectionDataDelegate protocol and use the NSURLConnection class.

 -(void) requestPage { NSString *urlString = @"http://the.page.you.want.com"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f]; responseData = [[NSMutableData alloc] init]; connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain]; delegate = target; } -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([response isKindOfClass:[NSHTTPURLResponse class]]) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; //If you need the response, you can use it here } } -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [responseData release]; [connection release]; } -(void) connectionDidFinishLoading:(NSURLConnection *)connection { if (connection == adCheckConnection) { NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //You've got all the data now //Do something with your response string [responseString release]; } [responseData release]; [connection release]; } 
+6
source share

If you are looking for a way to make HTTP requests to the server, there are a number of frameworks to help you with this. Until recently, ASIHTTPRequest was the one I approved of, but unfortunately it was discontinued.

The Google API client library is also a good option, as well as several others suggested by the creator of ASIHTTPRequest.

There must be a lot of paperwork to get you started.

EDIT: To make your request using ASIHTTPRequest , follow these steps in a class with the ASIHTTPRequestDelegate protocol:

 /** * Make a request to the server. */ - (void) makeRequest { // compile your URL string and make the request NSString *requestURL = [NSString stringWithFormat:@"%@?u=%@& p=%@&platform=ios", url, txtUserName.text, txtPassword.text]; NSURL *url = [NSURL URLWithString:requestURL]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } /** * Handle the response from the previous request. * @see ASIHTTPRequestDelegate */ - (void) requestFinished:(ASIHTTPRequest*)request { NSString *responseString = [request responseString]; // do whatever you need with the response, ie // convert it to a JSON object using the json-framework (https://github.com/stig/json-framework/) } 
0
source share
  • visit HTTPRequest and configure it in your project according to the given instructions.
  • TouchJSON - Download and put it in your project.
  • The following are the methods you can use to send a request and receive a response.

      -(void) SendAsyncReq:(NSString *)urlString { NSLog(@"URL IS: %@",urlString); NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url]; [request1 setDelegate:self]; [request1 startAsynchronous]; } /// THIS METHOD WILL BE CALLED IF REQUEST IS COMPLETED SUCCESSFULLY - (void)requestFinished:(ASIHTTPRequest *)response { NSData *responseData = [response responseData]; CJSONDeserializer *jsonDeserializer = [CJSONDeserializer deserializer]; NSDictionary *resultsDictionary = [jsonDeserializer deserializeAsDictionary:responseData error:nil]; NSLog(@"Response is: %@",resultsDictionary); } /// THIS METHOD WILL BE CALLED IF REQUEST IS FAILED DUE TO SOME REASON. - (void)requestFailed:(ASIHTTPRequest *)response { NSError *error = [response error]; NSLog(@"%d", [error code]); if([error code] !=4) { NSString *errorMessage = [error localizedDescription]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; [alertView release]; } } 

You must import #import "CJSONDeserializer.h" and #import "ASIHTTPRequest.h" into your class. Hope this helps.

0
source share