2011-11-11 15 views
4

私のURLがユーザ名とパスワードで認証されている間、私はXML解析をしています。XMLを認証してiphoneで解析する

このURLをブラウザに挿入すると、ログイン時にユーザー名とパスワードを要求されます。

私はこのURLをiphoneで解析したいと思います。NSXMLParserです。

私は以下のコードを使用していますが、私にはparseErrorOccurredというエラーが返されています。

NSString *UploadCardDesign=kLOGIN_URL; 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:UploadCardDesign]] autorelease]; 

[request setHTTPMethod: @"GET"]; 
[request setHTTPBody:nil]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
//NSString *strRequ=[[NSString alloc] initWithData:returnData encoding:nsen] 
NSString *data=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding]; 

と解析のために:

xmlParser = [[NSXMLParser alloc] initWithData:returnData]; 
[xmlParser setDelegate:self]; 
[xmlParser parse]; 

同じのために私を助けたり、私は良い結果のために何をすべきか教えてください。

答えて

2

認証のために、このデリゲートの機能を実装し....

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
if ([challenge previousFailureCount] == 0) 
{ 
    NSLog(@"received authentication challenge"); 

    NSURLCredential *newCredential; 
    newCredential=[NSURLCredential credentialWithUser:@"root" 
      password:@"rootpassword" 
      persistence:NSURLCredentialPersistenceForSession]; 


    NSLog(@"credential created"); 

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

    NSLog(@"responded to authentication challenge"); 

} 
else 
{ 
    NSLog(@"previous authentication failure"); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
       message:@"Website did not accept the username and password." 
       delegate:nil 
      cancelButtonTitle:@"OK" 
      otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
} 
+0

をどこ認証のため、この関数を呼び出すには? –

+0

あなたのNSURLConnectionデリゲートであるクラスにこれを入れてください... –

関連する問題