首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用一个NSURLConnection向web服务器发出多个经过身份验证的请求?

如何使用一个NSURLConnection向web服务器发出多个经过身份验证的请求?
EN

Stack Overflow用户
提问于 2012-08-19 22:58:48
回答 1查看 293关注 0票数 0

我想从http://mycompany.com/jsonpage1..etc获取JSON。for服务器需要初始登录http://mycompany.com/login,之后将为用户维护cookie。如何在不每次都要求登录的情况下使用NSURLCredentialStorage获得此行为?以下是使用NSURLCredential存储的非工作代码

代码语言:javascript
复制
- (IBAction)getJSON:(id)sender
{

//  [self establishSession];

NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user"
                                                         password:@"pass"
                                                          persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:@"myCompany.com"
                                         port:0
                                         protocol:@"http"
                                         realm:nil
                                         authenticationMethod:nil];

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
                                                    forProtectionSpace:protectionSpace];


//////////GET JSON//////////////

NSError *error;
NSURL *url = [NSURL URLWithString:@"http://mycompany.com.jsonpage1"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

 //I am NOT getting JSON in this delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString *responseString = [[NSString alloc] initWithData:responseData    encoding:NSUTF8StringEncoding];

NSLog(@"%@",responseString);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-19 23:06:30

您可以尝试使用AFNetworking库和子类AFHTTPClient,您可以在其中为每个请求封装身份验证。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12027465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档