【发布时间】:2011-07-31 08:53:56
【问题描述】:
我的应用程序中出现 NSURLConnection 问题已经苦苦挣扎了几天。我需要连接到服务器(调用 getPdfFromServerAtIndex),使用 ftp 协议来获取对象(pdf 页面,但没关系!)。当我尝试连接到 http url 时,我没有问题。对于要求身份验证的 http url,同样的事情,我的所有委托方法都会被调用。
但是当我使用下面显示的 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?!请帮忙。
标签: iphone xcode ftp nsurlconnection