PDA

View Full Version : Http request and response




rup28aug
Dec 17, 2008, 04:32 AM
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)



detz
Dec 17, 2008, 06:42 AM
This is about as simple as I can make it.

NSString *urlString = [NSString stringWithFormat:@"http://google.com"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *results = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

rup28aug
Dec 17, 2008, 08:07 AM
sir ,
thanks for your reply.
i want to like to use POST method . so can you provide some snippet related to post method

dejo
Dec 17, 2008, 09:51 AM
sir ,
thanks for your reply.
i want to like to use POST method . so can you provide some snippet related to post method
Umm... maybe try
[request setHTTPMethod:@"POST"];

x11joex11
Dec 17, 2008, 09:06 PM
Umm... maybe try
[request setHTTPMethod:@"POST"];

I LOL'ed at your response, hehe. Especially after seeing 'GET' in his code. Sometimes the answer is right in front of our noses :)

rup28aug
Dec 18, 2008, 12:20 AM
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?

Pring
Dec 18, 2008, 04:17 AM
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.

detz
Dec 18, 2008, 04:19 AM
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

dejo
Dec 18, 2008, 09:46 AM
so how can i set method and parameter in POST method?
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="

sujithkrishnan
Dec 22, 2008, 03:41 AM
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)

[request setHTTPBody:<whatever u want to send as post (NSData type)>];
[request setHTTPMethod: @"POST"];