sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

Sending Request and Get Response

I have a php code running on my server that i call my web service.It processes the data in send integer value.How can i get that?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 register my users and returns value like 1(for success,2 for duplicate).I need to send 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 server and get the return value from server?

Upvotes: 1

Views: 8298

Answers (3)

Kristofer Sommestad
Kristofer Sommestad

Reputation: 3061

If you're looking for a way to make HTTP requests to a server, there's a number of frameworks that's helping you do that. Up until recently, ASIHTTPRequest was the one i favored, but unfortunately it's been discontinued.

Google's API Client Library is also a good option, as well as a number of others suggested by the creator of ASIHTTPRequest.

There should be plenty of docs in there to get you started.

EDIT: To make your specific request with ASIHTTPRequest, do the following in a class with the protocol ASIHTTPRequestDelegate:

/**
 * 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, i.e. 
    // convert it to a JSON object using the json-framework (https://github.com/stig/json-framework/) 
}

Upvotes: 0

waheeda
waheeda

Reputation: 1097

  1. visit HTTPRequest and setup it in your project according to given instructins.
  2. TouchJSON - download and put it in your project.
  3. below are the methods you can use for sending request, and getting 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" in your class. I hope this will help.

Upvotes: 0

James Webster
James Webster

Reputation: 32066

You can use NSURLConnection. You should use implement 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];
}

Upvotes: 6

Related Questions