Objective-C与Cookie的asynchronousWeb请求
我在Objective-C中编写一个程序,我需要向web服务器发出web请求,但是asynchronous地,我在mac上是相当新的,我非常擅长windows技术,但是我需要知道如果我使用NSOperation在10.5中,我假设它不会在10.4 MAC中运行?),或者如果它被实现为使用在10.4上可用的系统线程,
或者我应该创build一个新的线程,并创build一个新的runloop,也如何使用cookie等,如果任何人都可以给我一个小例子,这将是很大的帮助。 如果可能的话,我也希望这个示例在mac 10.4上运行。
有一个很好的例子,使用NSURLRequest和NSHTTPCookies来做一个完整的Web应用程序的例子,login到一个网站,存储SessionID cookie并重新提交在未来的请求。
NSURLConnection,NSHTTPCookie
按logix812:
NSHTTPURLResponse * response; NSError * error; NSMutableURLRequest * request; request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://temp/gomh/authenticate.py?setCookie=1"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60] autorelease]; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]); // If you want to get all of the cookies: NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://temp"]]; NSLog(@"How many Cookies: %d", all.count); // Store the cookies: // NSHTTPCookieStorage is a Singleton. [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://temp"] mainDocumentURL:nil]; // Now we can print all of the cookies we have: for (NSHTTPCookie *cookie in all) NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate); // Now lets go back the other way. We want the server to know we have some cookies available: // this availableCookies array is going to be the same as the 'all' array above. We could // have just used the 'all' array, but this shows you how to get the cookies back from the singleton. NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://temp"]]; NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies]; // we are just recycling the original request [request setAllHTTPHeaderFields:headers]; request.URL = [NSURL URLWithString:@"http://temp/gomh/authenticate.py"]; error = nil; response = nil; NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]);
对于asynchronous请求,您需要使用NSURLConnection
。
对于cookie,请参阅NSHTTPCookie
和NSHTTPCookieStorage
。
更新:
下面的代码是真实的,来自我的一个应用程序的工作代码。 responseData
在类接口中定义为NSMutableData*
。
- (void)load { NSURL *myURL = [NSURL URLWithString:@"http://stackoverflow.com/"]; NSURLRequest *request = [NSURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [responseData release]; [connection release]; // Show error message } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // Use responseData [responseData release]; [connection release]; }
我能够以这种方式获取cookie:
NSArray* arr = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString: @"http://google.com" ]];
这也适用于asynchronous请求。