我已经检查了一些关于实现此目的的旧帖子,但尚未找到解决方案。我想知道是否可以在不使用第三方框架的情况下执行此操作。如果NSURLSession可以帮助满员。我尝试过如下操作,但不支持URL错误:
错误提交请求:可选(错误域= NSURLErrorDomain代码= -1002“不支持的URL”UserInfo = {NSUnderlyingError = 0x7feb907f2a80 {错误域= kCFErrorDomainCFNetwork代码= -1002“(null)”},NSErrorFailingURLStringKey = ftp.name.com ,NSErrorFailingURLKey =,NSLocalizedDescription =不支持的URL})
我的代码: 让urlString =“ftp.name.com”
let fileData = NSData(contentsOfFile: xmlFile.stringValue)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "user:password"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedDataWithOptions(.Encoding64CharacterLineLength)
let authString = "Basic \(base64EncodedCredential)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)
if let percentURLString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
{
let url = NSURL(string: percentURLString )
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.HTTPBody = fileData
print("FTP Request :\(request)")
let task = session.dataTaskWithRequest(request, completionHandler:
{
(data, response, error) in
if (error != nil)
{
print("error submitting request: \(error)")
return
}
if let httpResponse = response as? NSHTTPURLResponse
{
if httpResponse.statusCode != 200
{
print("response was not 200: \(response)")
return
}
else if httpResponse.statusCode == 200
{
print("FIle sent")
}
}
})
task.resume()
}
}