或者我应该创建一个新的线程,创建一个新的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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)