Shah
Shah

Reputation: 5020

HTTP Connection in Iphone

Since I'm new in iphone I need help in learning about http Connections in Iphone. Please guide me to some tutorials so that I could learn some http connections tricks in iphone and could communicate with websites.

Also please guide me through some Sample code for the following problem: I want to send some "Hello world" text to the site and got a reply back from the Web the same word with adiition of s i.e. "Hello Worlds".. please brothers guide me through any sample code.. it's a part of my application that I'm developing in iphone.

Upvotes: 1

Views: 1449

Answers (3)

Vasu Ashok
Vasu Ashok

Reputation: 1413

i too new ,but i have sent some msg(soap xml) via http request .my sample code is...

// get host address NSURL *url = [NSURL URLWithString:@"http://xyz.com"]; NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:url];

// find msg length, here x is my msg NSString *msgLength = [NSString stringWithFormat:@"%d", [x length]];

// specify type of msg which u have to send to the host(mine is xml) [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; // name space which optional one [theRequest addValue: @"http://mpack.h.org" forHTTPHeaderField:@"SOAPAction"];

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; //type of http method [theRequest setHTTPMethod:@"POST"]; // msg which is need to send to server [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

// establish connection to host NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection ) { webData = [[NSMutableData data] retain]; NSLog(@"Connecting....");

} else { NSLog(@"theConnection is NULL");

}

}

// delegate method for NSURLConnection -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength: 0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"ERROR with theConenction %@",error); [connection release]; [webData release];

} // u ll get response here -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"DONE. Received Bytes: %d", [webData length]);

}

Upvotes: 0

taskinoor
taskinoor

Reputation: 46037

The best resource is the URL Loading System Programming Guide from Apple. Using NSURLConnection section contains a number of sample codes.

Upvotes: 1

Di Wu
Di Wu

Reputation: 6448

You may check this. ASIHTTPRequest package is a highly recommended tool for handling traffic on a iPhone app.

http://allseeing-i.com/ASIHTTPRequest/How-to-use

Upvotes: 1

Related Questions