ios - 如何对 NSMutableURLRequest 发出第二个请求?

标签 ios objective-c iphone

我正在尝试使用 NSMutableURLRequest 与 session 建立连接。我正在开始第一个请求,我从中接收数据。但我不能提出第二个要求。如何使用同一个 cookie 发出第二个请求?

更新源代码:

NSHTTPURLResponse   * response;
NSError             * error;
NSMutableURLRequest * request;
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://loginsite"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                                    timeoutInterval:60];
NSString *post = [NSString stringWithFormat:@"userid=%@&pass=%@",self.userName.text, self.password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.urlConnection start];

它发出第一个请求,但我如何发出第二个请求?

最佳答案

Cookie 是一个 HTTP 东西,所以我假设我们正在谈论它。
1. 请求成功时,您会得到一个 NSURLResponse,它是一个 HTTPURLResponse,其 header 可以解析为 cookie:

NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:[self url]]];

这些 cookie 可以与 2. 请求一起使用...

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers];

关于ios - 如何对 NSMutableURLRequest 发出第二个请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13782008/

相关文章:

objective-c - -[UITableView layoutSublayersOfLayer :] 断言失败

iphone - 使用 Chipmunk 物理引擎创建椭圆

iphone - 具有 Xib 文件的 UIView 的子类

iphone - 自定义委托(delegate)未接听电话

iphone - 未调用 viewForZoomingInScrollView 以支持缩放

iphone - 核心位置第一点无效

ios - EC2 上的 APNS 不工作

iphone - 如何处理使用 ios5 中的调度方法将记录上传到服务器

ios - iOS 开发在数据收集方面的限制是什么?

ios - 图像未显示在我的 UICollectionView 中