iphone - ASIHttprequest 用于登录认证

标签 iphone ios web-services authentication asihttprequest

我正在使用 ASIHttprequest 进行登录身份验证 Here is the deme code

我正在尝试的代码是:

[self setRequest:[ASIHTTPRequest requestWithURL:url1]];
[_request setUseKeychainPersistence:[useKeychain isOn]];
[_request setDelegate:self];
[_request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[_request setDidFinishSelector:@selector(topSecretFetchComplete:)];
[_request setDidFailSelector:@selector(topSecretFetchFailed:)];
[_request startAsynchronous];
[request setValidatesSecureCertificate:NO];
[_request setValidatesSecureCertificate:NO];

请求失败和请求完成方法

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{
   [responseField setText:[[request error] localizedDescription]];
   [responseField setFont:[UIFont boldSystemFontOfSize:12]];
}

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
   [responseField setText:[request responseString]];
   [responseField setFont:[UIFont boldSystemFontOfSize:12]];
   NSLog(@"Response :%@",[request responseString]);

- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];

[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
[alertView show];

我是 ios 的新手所以请......
在此演示中,当我走钟时会弹出一个警报,但是当我使用我的网络服务时,它会给出这样的响应

Response :ASIHTTPRequest: 0xa846400

但不要弹出输入用户名和密码的警告 View

如果有任何其他更好的方法来验证登录,请建议我

我想以rootview登录,成功登录后可以进入应用

谢谢...

最佳答案

有时会出现空格、特殊字符和换行符在响应中的情况,因此首先修剪该字符串,然后像下面这样打印响应。

NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Response :%@",strResponse);

更新:

- (NSString *)flattenHTML:(NSString *)html {

    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString:@"<" intoString:NULL] ; 

        [theScanner scanUpToString:@">" intoString:&text] ;

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    //
    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    return html;
}

像下面这样使用这个方法..

 NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 NSString *strFinal = [self flattenHTML:strResponse];
 NSLog(@"Response :%@",strFinal);

关于iphone - ASIHttprequest 用于登录认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988312/

相关文章:

ios - Xcode 6 beta 2 问题导出 .ipa : "Your account already has a valid iOS distribution certificate"

iphone - 使用 JSON 问题在 UITableView 中导航缓慢

ios - 如何在家庭网络之外的设备上运行我的 react-native 应用程序?

ios - 如何在 Swift 3.0 中调用 REST 服务

java - 在亚马逊云上使用 Java Web 服务

web-services - 在 Web 服务中,端点和远程网关有什么区别

iphone - iOS - 在导航更改时停止视频

ios - 如何释放 AudioFileReadPacketData 使用的内存 - Xcode 显示内存仍在使用中

web-services - 如何使用未知证书调用 Web 服务

android - 关于3G的任何简单解读