Http request and response

Discussion in 'iOS Programming' started by rup28aug, Dec 17, 2008.

  1. macrumors newbie

    Joined:
    Oct 22, 2008
    #1
    hi all,
    i am new to iPhone programming.
    i want to access some function(REST) on the server. i have no idea that how can i request to this server function and get response?

    i want to simple code where i can supply server address and function name. Please suggest very simple way for doing this task(since i am on learning stage and very little knowledge)
     
  2. macrumors 65816

    Joined:
    Jun 29, 2007
    #2
    This is about as simple as I can make it.
    PHP:
    NSString *urlString = [NSString stringWithFormat:@"http://google.com"];
    NSMutableURLRequest *request = [[[NSMutableURLRequest allocinitautorelease];
    [
    request setURL:[NSURL URLWithString:urlString]];
    [
    request setHTTPMethod:@"GET"];
        
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *results = [[NSString allocinitWithData:returnData encoding:NSUTF8StringEncoding];
     
  3. thread starter macrumors newbie

    Joined:
    Oct 22, 2008
    #3
    sir ,
    thanks for your reply.
    i want to like to use POST method . so can you provide some snippet related to post method
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Umm... maybe try
    Code:
    [request setHTTPMethod:@"POST"];
     
  5. macrumors newbie

    Joined:
    Dec 17, 2008
    #5
    I LOL'ed at your response, hehe. Especially after seeing 'GET' in his code. Sometimes the answer is right in front of our noses :)
     
  6. thread starter macrumors newbie

    Joined:
    Oct 22, 2008
    #6
    sir,
    thanks for your reply.
    actually i want to call a method which take parameter and return result in XML format. so how can i set method and parameter in POST method?
     
  7. macrumors 6502

    Joined:
    Sep 17, 2003
    #7
    I wept when reading this...

    rup28aug, as an exercise perhaps you can explain what GET and POST actually mean? I think you should do some research before posting your questions.
     
  8. macrumors 65816

    Joined:
    Jun 29, 2007
    #8
    Well if you want to actually POST data and not just do a GET with a post method it's more complicated. Do a search, there are articles written on it, my code is to messy to post examples. :p
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    The "method" and parameters can be put into the request URL. For example, @"http://www.google.com/search?hl=en&q=iphone&btnG=Google+Search&aq=f&oq="
     
  10. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #10
    [request setHTTPBody:<whatever u want to send as post (NSData type)>];
    [request setHTTPMethod: @"POST"];
     

Share This Page