RESTãªãªã½ã¼ã¹ã¸ãªã¯ã¨ã¹ããéããã
NSMutableRequest#setHTTPMethod:ã§GET/POST/PUT/DELETEãæå®ãããã¨ã§RESTãªãªã½ã¼ã¹ã«å¯¾ãã¦ã®ãªã¯ã¨ã¹ããéä¿¡ãããã¨ãã§ããã
POSTã¨PUTã®æã«ã¯éä¿¡ããã³ã³ãã³ãã®Content-Typeãæå®ãããã¨ãå¿ããã«ã(ä»åã¯XMLãã¼ã¿ãéä¿¡ããã®ã§application/xmlãæå®ãã¦ãã)
// POST { NSURL *url = [NSURL URLWithString:@"http://example.com/users.xml"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setValue:@"application/xml"forHTTPHeaderField:@"Content-Type"]; [request setHTTPMethod:@"POST"]; NSString *content = @"<user><name>post</name><age>55</age></user>"; [request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]]; [NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]]; } // PUT { NSURL *url = [NSURL URLWithString:@"http://example.com/users/1.xml"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setValue:@"application/xml"forHTTPHeaderField:@"Content-Type"]; [request setHTTPMethod:@"PUT"]; NSString *content = @"<user><name>HOGE</name><age>99</age></user>"; [request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]]; [NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]]; } // GET { NSURL *url = [NSURL URLWithString:@"http://example.com/users.xml"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; [NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]]; } // DELETE { NSURL *url = [NSURL URLWithString:@"http://example.com/users/10.xml"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"DELETE"]; [NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]]; }
ãã¨ãåãåã£ãã¬ã¹ãã³ã¹ã表示ããããã®NSURLConnectionã®delegateã¯ã©ã¹ãå°å³ã«ä¾¿å©ãªã®ã§ããã¾ãã¨ãã¦ããã«è¼ãã¦ãããã
// ConnectionHandler.h #import <Foundation/Foundation.h> @interface ConnectionHandler : NSObject { NSMutableData *receivedData; NSStringEncoding receivedDataEncoding; } @end // ConnectionHandler.m #import "ConnectionHandler.h" @implementation ConnectionHandler - (id)init { self = [super init]; if (self) { receivedData = [[NSMutableData alloc] init]; } return self; } // ãµã¼ãããã¬ã¹ãã³ã¹ããããåãåã£ãã¨ãã«å¼ã³åºããã - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // éä¿¡ããããã¼ã¿ã®æåã³ã¼ããåå¾ NSString *encodingName = [response textEncodingName]; NSLog(@"åä¿¡æåã³ã¼ã: %@", encodingName); if ([encodingName isEqualToString: @"euc-jp"]) { receivedDataEncoding = NSJapaneseEUCStringEncoding; } else { receivedDataEncoding = NSUTF8StringEncoding; } } // ãµã¼ããããã¼ã¿ãåãåããã³ã«å¼ã³åºããã - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // åå¾ãããã¼ã¿ãreceivedDataã¸æ ¼ç´ãã NSLog(@"åä¿¡ãã¼ã¿ï¼ãã¤ãæ°ï¼: %d", [data length]); [receivedData appendData:data]; } // ãã¼ã¿ã®åå¾ãçµäºããã¨ãã«å¼ã³åºããã - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *result = [[NSString alloc] initWithData:receivedData encoding:receivedDataEncoding]; NSLog(@"ãã¼ã¿ã®åä¿¡å®äº: %@", result); [result release]; [receivedData release]; } @end