2011-04-07 2 views
0

저는 며칠 동안 내 응용 프로그램에서 NSURLConnection 문제로 고생하고 있습니다. 나는 (getPdfFromServerAtIndex를 호출하는) 서버에 연결할 필요가있다. ftp 프로토콜을 사용하여 객체를 얻는다. (pdf 페이지이지만 문제가되지 않는다.) http url에 연결하려고하면 아무런 문제가 없습니다. 인증을 요청하는 http url의 경우와 동일한 모든 위임 메서드가 호출됩니다.didReceiveAuthenticationChallenge가 ftp 프로토콜을 사용하여 호출되지 않습니다.

그러나 아래에 표시된 URL을 사용하면 FTP 프로토콜을 사용하여 작성된 didReceiveAuthenticationChallenge와 같은 메소드를 위임하고 canAuthenticateAgainstProtectionSpace가 호출되지 않습니다. (가) 루핑 "(! 완료) 동안"만 connectionShouldUseCredentialStorage 메서드를 호출하고, didFailWithError 방법보다 나에게 오류를 줄입니다! "연결이 오류를 실패했습니다. - 당신이 요청 된 리소스에 액세스 할 수있는 권한이 없습니다 ftp://ftp.www.thephilosopher.org/"

. 그럼에도 불구하고 내 브라우저에 FTP URL을 붙여 넣으려고하면 사용자 이름과 비밀번호를 입력해야합니다.

ftp 프로토콜을 사용하여 서버를 인증하고 액세스 할 수없는 이유는 무엇입니까?

-(void)getPdfFromServerAtIndex:(NSUInteger)index{ 

// Create the request. 
// Set finished to false 
finished = FALSE; 

NSString *pageNumber = [NSString stringWithFormat:@"ftp://ftp.www.thephilosopher.org/Iphone/0%i.pdf",index+1]; 

NSURL* nsurl = [NSURL URLWithString:pageNumber]; 
urlReq = [[NSMutableURLRequest alloc] initWithURL:nsurl]; 
BOOL canHandle = [NSURLConnection canHandleRequest:urlReq]; 


NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self]; 


if (conn && canHandle) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    receivedData = [[NSMutableData data] retain]; 
} else { 
    // Inform the user that the connection failed. 
    NSLog(@"Connection Failed!"); 
} 


while(!finished) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];} 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
finished = TRUE; 

// receivedData is declared as a method instance elsewhere 
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

// release the connection, and the data object 
[connection release]; 
} 

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)tmpProtectionSpace { 
// Check the NSURLProtectionSpace 
return YES;} 

-(NSURLRequest *)connection:(NSURLConnection *)inConnection willSendRequest:(NSURLRequest *)inRequest redirectResponse:(NSURLResponse *)inRedirectResponse{  
if(inRedirectResponse){ 
    NSMutableURLRequest *r = [[urlReq mutableCopy] autorelease]; // original request 
    [r setURL: [inRequest URL]]; 
    return r; 
} else { 
    return inRequest; 
} 
} 

-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection*)connection{ 
NSLog(@"connectionShouldUseCredentialStorage"); 
return YES; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ 
if([challenge previousFailureCount] == 0) { 
    NSURLCredential *newCredential; 
    newCredential=[NSURLCredential credentialWithUser:@"XXXX" password:@"XXXXX" persistence:NSURLCredentialPersistenceNone]; 
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];} 
else { 
[[challenge sender] cancelAuthenticationChallenge:challenge]; 
NSLog(@"Bad Username Or Password");} 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
// release the connection, and the data object 
[connection release]; 
// receivedData is declared as a method instance elsewhere 
[receivedData release]; 

// inform the user 
NSLog(@"Connection failed! Error - %@ %@", 
     [error localizedDescription], 
     [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

NSLog(@"Data received"); 
[receivedData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
NSLog(@"Response received"); 
[receivedData setLength:0]; 
} 
+0

내가있어 업데이 트를. ASIFormDataRequest를 사용하여 사용자 이름과 암호를 사용하여 서버에 쉽게 액세스 할 수 있습니다. NSURLConnection과 함께 작동하지 않는 이유는 무엇입니까? 도와주세요. – DennyLou

답변

관련 문제