0

我有这段代码试图使用 URL 访问 FTP。但我无法访问,因为它受用户名和密码保护。我如何实现它以便我可以进入服务器?这是我的代码:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse  *)response
{

[receivedData setLength:0];
}

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

[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{


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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// 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];
//[receivedData release];
}



-(IBAction)getURL:(id)sender {


NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL     URLWithString:@"ftp://10.0.1.***/App_Data/text.txt"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest     delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [NSMutableData data];
} else {
    // Inform the user that the connection failed.
    NSLog(@"connection failed");
}}
4

1 回答 1

1

用户 ID 和密码可以嵌入到 URL 中:

ftp://userid:password@10.0.1.***/App_Data/text.txt
于 2012-07-27T18:33:24.193 回答