连接到iOS中的服务器未调用DiReceiveAuthenticationChallenge?
我正在尝试连接到iOS应用程序中的服务器。这是我的密码:连接到iOS中的服务器未调用DiReceiveAuthenticationChallenge?,ios,authentication,networking,nsurlconnection,basic-authentication,Ios,Authentication,Networking,Nsurlconnection,Basic Authentication,我正在尝试连接到iOS应用程序中的服务器。这是我的密码: - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ NSLog(@"This is happening"); NSURLCredential *newCredential = [NSURLCredential credent
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"This is happening");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"user"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];
NSLog(@"%i",code);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr);
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
NSLog(@"here...");
return YES;
}
这在viewDidLoad中:
NSString *url = @"http://example.com:9999/";
NSURL * URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:URL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection self];
这是代表的标题:
<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
未调用didReceiveAuthenticationChallenge,但调用了didReceiveResponse并打印“401”,并且didReceiveData返回“需要验证”页面上的文本
didReceiveAuthenticationChallenge从未调用,CanAuthenticationAgainstProtectionSpace也未调用,因此我无法进行身份验证。如何解决此问题?要调用此方法,必须在url中使用https而不是http
NSString *url = @"https://example.com:9999/";
NSURL * URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:URL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection self];
我认为你错过了一位代表
NSURLAuthenticationChallengeSender是否调用了
CanAuthenticationAgainstProtectionSpace
?我应该使用什么来代替?啊,我明白了。不幸的是,我正在连接的站点没有https设置,只有HTTP。有没有其他方法可以连接到常规http?从来没有让它只在http上工作,而且考虑到使用这种身份验证方法(基本身份验证),您的凭据会随每个请求一起发送,不加密是一个相当大的安全漏洞,这是一个巨大的安全漏洞。我同意。我们只是想看看它现在是否连接,这是下一步。您可以在这里尝试联网,它提供了一些方便的方法来根据请求设置基本身份验证头([httpClient setAuthorizationHeaderWithUsername:_UserNamePassword:_password];)