我一直在我的應用程序的NSURLConnection問題掙扎了幾天。我需要連接到一個服務器(調用getPdfFromServerAtIndex),使用ftp協議來獲得一個對象(一個pdf頁面,但沒關係!)。當我嘗試連接到一個http url時,我沒有問題。同樣的事情需要一個http url請求認證,所有我的委託方法被調用。didReceiveAuthenticationChallenge沒有被調用使用ftp協議
但是,當我使用下面顯示的URL,使用ftp協議構建,像didReceiveAuthenticationChallenge,canAuthenticateAgainstProtectionSpace這樣的委託方法永遠不會被調用。
當「while(!finished)」正在循環時,僅調用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];
}
我已經得到了更新。使用ASIFormDataRequest,我可以使用用戶名和密碼輕鬆訪問服務器。爲什麼它不適用於NSURLConnection?請幫忙。 – DennyLou 2011-04-07 19:42:32