我是iOS和https的新手。我是一个初学软件的学生。我想写一个应用程序,从我的uni帐户中提取数据(如班号,公告等),并在我的应用程序上显示。
通常,我打开我的大学学生页面,用我的用户名和密码登录,这些信息就会显示在浏览器上。
我尝试过使用NSURLConnection和它的代理。请看我下面的代码。一开始,在connection:didReceviveAuthenticationChallenge,方法中,我通过使用#en0#:password: NSURLCredential创建了一个NSURLCredential对象。但它并没有起作用。然后我试着做NSLog,然后我才发现protectionSpace.authenticationMethod是NSURLAuthenticationMethodServerTrust。
然后,我试着阅读了苹果的文档和一些谷歌资源。但是,我真的不能得到它,然后我编辑了我的代码,就像你在下面看到的(这是我的最终版本,它仍然不能工作)。
我真的不明白的一点是:我希望服务器应该要求我提供用户名和密码。相反,它要求credentialForTrust,根据我读过的那些文档,他们建议我评估NSURLConnection委派对服务器的信任。然而,服务器从未要求提供用户名和密码。那么,服务器如何知道我正在访问哪个帐户。
因此,我认为证书(信任)与用户名和密码之间应该存在某种关系。我真的不明白这些东西是怎么工作的。
我想,我的问题可能不是很清楚,也可能是愚蠢的。我接受这一点,因为我从来没有学过所有这些东西。
所以,请有人给我解释一下这些东西是如何工作的。你可以假设我对什么是https,SSL,NSURLConnection,NSURLCredential等有一些基本的了解,请指导我找到解决方案。
我很感谢你所做的一切努力。
下面是我的代码(它不工作)。
connection:didReceiveAuthenticationChallenge中的NSLog(),输出"NSURLAuthenticationMethodServerTrust“
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"authenticationMethod is: %@\n",challenge.protectionSpace.authenticationMethod);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}else{
NSURLCredential *creden = [[NSURLCredential alloc] initWithUser:myusername password:mypassword persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:creden forAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@\n %@\n",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.myData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.myData setLength:0];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *str = [[NSString alloc] initWithData:self.myData encoding:NSASCIIStringEncoding];
NSLog(@"Data in String %@", str);
}发布于 2013-07-11 01:53:23
当URL加载系统不信任服务器时,就会出现NSURLAuthenticationMethodServerTrust问题。如果您希望避免这些调用,请确保iOS信任您服务器的证书(by installing it manually,或者让像Verisign这样的受信任根对其进行签名)。
https://stackoverflow.com/questions/11223926
复制相似问题