Graphics Programs Reference
In-Depth Information
dicates that the client wants something from the server. The thing that it wants is called the
Request-URI ( smartfeed.php?limit=1_DAY&etc ).
In the early days of the web, the Request-URI would be the path of a file on the server.
For example, the request http://www.website.com/index.html would return
the file index.html , and your browser would render that file in a window. Today, we
also use the Request-URI to specify a service that the server implements. For example, in
this chapter, you accessed the smartfeed.php service, supplied parameters to it, and
were returned an XML document. You are still GET ting something, but the server is more
clever in interpreting what you are asking for.
In addition to getting things from a server, you can send it information. For example,
many web servers allow you to upload photos. A client application would pass the image
data to the server through a service request. In this situation, you use the HTTP method
POST , which indicates to the server that you are including the optional HTTP body. The
body of a request is data you can include with the request - typically XML, JSON, or
Base-64 encoded data.
When the request has a body, it must also have the Content-Length header. Handily
enough, NSURLRequest will compute the size of the body and add this header for you.
NSURL *someURL = [NSURL URLWithString:@"http://www.photos.com/upload"];
UIImage *image = [self profilePicture];
NSData *data = UIImagePNGRepresentation(image);
NSMutableURLRequest *req =
[NSMutableURLRequest requestWithURL:someURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:90];
// This adds the HTTP body data and automatically sets the Content-Length header
[req setHTTPBody:data];
// This changes the HTTP Method in the request-line
[req setHTTPMethod:@"POST"];
// If you wanted to set the Content-Length programmatically...
[req setValue:[NSString stringWithFormat:@"%d", [data length]]
forHTTPHeaderField:@"Content-Length"];
Search WWH ::




Custom Search