在我的应用中,我一直在努力解决NSURLConnection问题。我需要连接到服务器(调用getPdfFromServerAtIndex),使用ftp协议来获取一个对象(一个pdf页面,但这没关系!)。当我尝试连接到http网址时,我没有问题。对于要求身份验证的http url,同样的事情,我的所有委托方法都被调用。
但是当我使用下面显示的url时,使用ftp协议构建,委托像didReceiveAuthenticationChallenge,canAuthenticateAgainstProtectionSpace这样的方法永远不会被调用。
当“while(!finished)”循环时,仅调用connectionShouldUseCredentialStorage方法,并且didFailWithError方法给出错误“连接失败!错误 - 您无权访问所请求的资源。{{3 }}”。然而,当我尝试在我的浏览器上粘贴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 :(得分:0)
以“ftp:// username:password @ ipaddress / filepath /”
格式发出NSURL在你的情况下:ftp://username:password@ftp.www.thephilosopher.org/Iphone/0%i.pdf