Objective-C异步Web请求与Cookie

Objective-C异步Web请求与Cookie,第1张

概述我正在Objective-C中编写一个程序,我需要将Web请求发送到Web服务器,但异步地,我在Mac上是相当新的,我非常擅长 windows技术,但是我需要知道,如果我使用NSOperation在10.5中,我假设它不会在10.4 MAC中运行),或者如果它被实现,使用将在10.4上可用的系统线程? 或者我应该创建一个新的线程,创建一个新的runloop,还有如何使用cookies等,如果有人可 我正在Objective-C中编写一个程序,我需要将Web请求发送到Web服务器,但异步地,我在Mac上是相当新的,我非常擅长 windows技术,但是我需要知道,如果我使用NSOperation在10.5中,我假设它不会在10.4 MAC中运行),或者如果它被实现,使用将在10.4上可用的系统线程?

或者我应该创建一个新的线程,创建一个新的runloop,还有如何使用cookies等,如果有人可以给我一个小例子,那将是很大的帮助.我想要这个样本也可以在mac 10.4上运行.

解决方法 有一个很好的例子,使用NSURLRequest和NShttpcookies做一个完整的Web应用程序登录网站的例子,存储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]);
总结

以上是内存溢出为你收集整理的Objective-C异步Web请求与Cookie全部内容,希望文章能够帮你解决Objective-C异步Web请求与Cookie所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1252277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存