views:

734

answers:

3

I want to connect to a server using HTTP, send a string to it and recieve the response string from the server. Any ideas about how to connect to server by the url, send and recieve message from it?

+1  A: 

You can connect to a server, send a string, and receive the response using the NSMutableURLRequest and NSURLConnection classes. A good place to start is the Introduction to the URL Loading System.

Gordon Wilson
+3  A: 
// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                    timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}

See this resource.

Kai
+4  A: 

I suggest using the excellent ASIHTTPRequest source from All-Seeing Interactive: http://allseeing-i.com/ASIHTTPRequest. I'm doing this, and so are several released iPhone apps, so you can be sure the code is pretty solid.

This is a wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.

It is suitable for performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.

Jane Sales
It will be nice to know how to do the work with specific codes when I'm learning. After that, I may wish to make the work simple using open source codes. Thanks anyway.
iPhoney