2011-10-12 52 views
0

我有一个休息的服务,(服务URL是一个虚拟的一个&不工作)身份验证REST服务从IOS应用

http://www.testDomain.com/authenticate/

这是用户名和密码来验证

如何我是否从我的应用程序中调用此休息服务?

下面是我写的,以消耗该REST服务代码(早些时候服务没有用户名密码&认证)。

NSString * testUrl = @“http://www.testDomain.com/authenticate/”;

 //NSString *[email protected]"http://www.testDomain.com/authenticate/"; 

     NSMutableURLRequest *request=[[[NSMutableURLRequest alloc]init]autorelease]; 
     [request setHTTPMethod:@"POST"]; 
     [request setURL:[NSURL URLWithString:testUrl]]; 
     [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

     //[request addValue:<#(NSString *)value#> forHTTPHeaderField:<#(NSString *)field#>] 
     [request setHTTPBody:bodyData]; 
+0

链接无法访问,是否受基本身份验证保护? – Thys

+0

@我如何从我的IOS应用程序(我的用户名和密码)完成这个基本的身份验证过程? –

回答

1

有两种方式: 不安全: 询问用户的用户名,并通过发送URL作为http://username:[email protected] ....

支持NSURLConnection的委托:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    return YES; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    NSString *user = [NSString stringWithFormat:@"%c%s%@", 'a', "a", @"a"]; 
    NSString *password = [NSString stringWithFormat:@"%c%s%@", 'a', "a", @"a"]; 

    NSURLCredential *credential = [NSURLCredential credentialWithUser:user 
                  password:password 
                  persistence:NSURLCredentialPersistenceForSession]; 
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 

} 

的ü服务更好地使用第一种方式,反正bcdü不保护链接通过ssl和某人智能可以嗅探所有用户和密码:) ps有趣的字符串是防止读取登录/传递代码...