我一直在我的应用程序的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