请求使用Windows身份验证的网页 [英] request a webpage that uses windows authentication

查看:74
本文介绍了请求使用Windows身份验证的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是目标c的新手,我想做的是请求使用网络凭据身份验证的网页的源代码.我曾经在C#中使用httpwebrequest和网络凭据来做到这一点,但是任何想法如何在目标C中做到这一点.

I''m new to objective c and what i''m trying to do is request the source code of a webpage that uses network credentials authentication. I used to do it in C# using httpwebrequest and network credentials but any ideas how to do this in objective C.

推荐答案

对于身份验证,您需要使用NSUrlConnection及其委托方法.

委托方法:

-(void)连接:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)响应
{
[取消连接]; //连接是一个NSUrlConnection对象
connection = nil;

//在这里做您的代码
}


-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
返回NO;
}


-(BOOL)connection:(NSURLConnection *)connection可以验证AgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
如果([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic] ||
[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest])
{
//NSLog(@"NSURLAuthenticationMethodHTTPBasic");
如果(nil!= usernameTextField.text)
返回YES;

[self showAlertView];
[连接取消];
连接=零;
返回NO;
}
其他
{
//NSLog(@"NSURLAuthenticationMethodClientCertificate或NSURLAuthenticationMethodServerTrust");
返回NO;
}
返回YES;
}


-(无效)连接:(NSURLConnection *)连接didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)挑战
{
如果([挑战previousFailureCount] == 0)
{
NSURLCredential * newCredential;

newCredential = [NSURLCredential credentialWithUser:usernameTextField.text

密码:passwordTextField.text

持续性:NSURLCredentialPersistencePermanent];

[[挑战发件人] useCredential:newCredential forAuthenticationChallenge:challenge];

[usernameTextField版本],usernameTextField = nil;
[passwordTextField发布],passwordTextField = nil;
}
其他
{
[[挑战发件人] cancelAuthenticationChallenge:challenge];
//通知用户用户名和密码
//在首选项中不正确
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Alert"消息:@首选项凭据不正确"委托:无cancelButtonTitle:@"OK" otherButtonTitles:无,无];
[alertView show];
[alertView版本];
}
}


-(无效)连接:(NSURLConnection *)连接didFailWithError:(NSError *)错误
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@无法打开页面"
消息:@服务器无响应"
代表:无cancelButtonTitle:@确定"
otherButtonTitles:nil];

[警报显示];
[警报发布];
}


-(void)showAlertView
{
NSString * messageString = connectionString;
如果(messageString.length> 32)
messageString = [messageString substringToIndex:32];
messageString = [messageString stringByAppendingString:@"\ n \ n \ n \ n"];

if(nil!= authenticateAlertView)
[authenticateAlertView版本],authenticateAlertView = nil;

authenticateAlertView = [[UIAlertView分配] initWithTitle:@需要验证"消息:messageString委托:self cancelButtonTitle:@取消" otherButtonTitles:@登录",无];
authenticateAlertView.tag = 1001;
[authenticateAlertView显示];

如果(nil!= usernameTextField)
[usernameTextField版本],usernameTextField = nil;
usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(14,75,255,25)];
usernameTextField.placeholder = @用户名";
usernameTextField.backgroundColor = [UIColor clearColor];
[usernameTextField setBorderStyle:UITextBorderStyleRoundedRect];
usernameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameTextField.keyboardType = UIKeyboardTypeAlphabet;
usernameTextField.returnKeyType = UIReturnKeyDefault;
usernameTextField.delegate = nil;
usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

如果(nil!= passwordTextField)
[passwordTextField发布],passwordTextField = nil;
passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(14,105,255,25)];
passwordTextField.placeholder = @密码";
passwordTextField.backgroundColor = [UIColor clearColor];
[passwordTextField setBorderStyle:UITextBorderStyleRoundedRect];
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordTextField.secureTextEntry =是;
passwordTextField.keyboardType = UIKeyboardTypeAlphabet;
passwordTextField.returnKeyType = UIReturnKeyDefault;
passwordTextField.delegate = nil;
passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

[authenticateAlertView addSubview:usernameTextField];
[authenticateAlertView addSubview:passwordTextField];
}
For authentication u need to use NSUrlConnection and its delegate methods.

Delegate methods:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[connection cancel]; // connection is an NSUrlConnection object
connection=nil;

// Do ur code here
}


-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return NO;
}


-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)protectionSpace
{
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic] ||
[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest])
{
//NSLog(@"NSURLAuthenticationMethodHTTPBasic");
if (nil != usernameTextField.text)
return YES;

[self showAlertView];
[connection cancel];
connection = nil;
return NO;
}
else
{
//NSLog(@"NSURLAuthenticationMethodClientCertificate or NSURLAuthenticationMethodServerTrust");
return NO;
}
return YES;
}


-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0)
{
NSURLCredential *newCredential;

newCredential = [NSURLCredential credentialWithUser:usernameTextField.text

password:passwordTextField.text

persistence:NSURLCredentialPersistencePermanent];

[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

[usernameTextField release], usernameTextField = nil;
[passwordTextField release], passwordTextField = nil;
}
else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
// inform the user that the user name and password
// in the preferences are incorrect
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Preferences Credentials Are Incorrect" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}
}


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Open Page"
message:@"Server is not responding"
delegate:nil cancelButtonTitle:@"Ok"
otherButtonTitles:nil];

[alert show];
[alert release];
}


-(void)showAlertView
{
NSString *messageString = connectionString;
if (messageString.length > 32)
messageString = [messageString substringToIndex:32];
messageString = [messageString stringByAppendingString:@"\n\n\n\n"];

if(nil != authenticateAlertView)
[authenticateAlertView release], authenticateAlertView = nil;

authenticateAlertView = [[UIAlertView alloc] initWithTitle:@"Authentication Required" message:messageString delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Log In", nil];
authenticateAlertView.tag = 1001;
[authenticateAlertView show];

if (nil != usernameTextField)
[usernameTextField release], usernameTextField = nil;
usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(14, 75, 255, 25)];
usernameTextField.placeholder = @"Username";
usernameTextField.backgroundColor = [UIColor clearColor];
[usernameTextField setBorderStyle:UITextBorderStyleRoundedRect];
usernameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameTextField.keyboardType = UIKeyboardTypeAlphabet;
usernameTextField.returnKeyType = UIReturnKeyDefault;
usernameTextField.delegate = nil;
usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

if (nil != passwordTextField)
[passwordTextField release], passwordTextField = nil;
passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(14, 105, 255, 25)];
passwordTextField.placeholder = @"Password";
passwordTextField.backgroundColor = [UIColor clearColor];
[passwordTextField setBorderStyle:UITextBorderStyleRoundedRect];
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordTextField.secureTextEntry = YES;
passwordTextField.keyboardType = UIKeyboardTypeAlphabet;
passwordTextField.returnKeyType = UIReturnKeyDefault;
passwordTextField.delegate = nil;
passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

[authenticateAlertView addSubview:usernameTextField];
[authenticateAlertView addSubview:passwordTextField];
}


这篇关于请求使用Windows身份验证的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆