2011-10-12 55 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有趣的字符串是防止讀取登錄/傳遞代碼...