请帮忙使用SFTP连接到我的服务器,我尝试了很多.但它不能正常工作.服务器工作正常.
//第一种方法
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "username : password"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
let authString = "Basic \(base64EncodedCredential)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)
let url = NSURL(string: "sftp.myserver.com")
let task = session.dataTaskWithURL(url!) {
(let data, let response, let error) in
// data - nil, response - nil
}
task.resume()
//第二种方法
func connect() {
let url: NSURL = NSURL(string: "sftp.myserver.com")!
let login:String = "username"
let password:String = "password"
let defaultCredentials: NSURLCredential = NSURLCredential(user: login, password: password, persistence: NSURLCredentialPersistence.ForSession)
let host: String = "myserver.com"
let port: Int = 22
let prot: String = "sftp"
let protectionSpace: NSURLProtectionSpace = NSURLProtectionSpace(host: host,port: port,`protocol`: prot,realm: nil,authenticationMethod: NSURLAuthenticationMethodServerTrust)
let credentialStorage: NSURLCredentialStorage = NSURLCredentialStorage.sharedCredentialStorage()
credentialStorage.setCredential(defaultCredentials, forProtectionSpace: protectionSpace)
let sessionConfiguration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
sessionConfiguration.URLCredentialStorage = credentialStorage
let session: NSURLSession = NSURLSession(configuration: sessionConfiguration)
let task = session.dataTaskWithURL(url, completionHandler: { data, response, _ -> Void in
// data- nil, response - nil
if let dataRRR = data, jsonResult = try! NSJSONSerialization.JSONObjectWithData(dataRRR, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
print(jsonResult)
}
})
task.resume()
}
//第三种方法
func heloo() {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration)
let login:String = "username"
let password:String = "password"
let params:[String: AnyObject] = [
"email" : login,
"userPwd" : password ]
let url = NSURL(string:"sftp.myserver.com")
let request = NSMutableURLRequest(URL: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions(rawValue: 0))
let task = session.dataTaskWithRequest(request) {
data, response, error in
// data - nil, response - nil
if let dataRRR = data, jsonResult = try! NSJSONSerialization.JSONObjectWithData(dataRRR, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
print(jsonResult)
}
}
task.resume()
}
当我在浏览器中加载这个“sftp.myserver.com”时,我得到像这个的登录表单
最佳答案 来自NSURLSession文档:
The NSURLSession class natively supports the data, file, ftp, http,
and https URL schemes, with transparent support for proxy servers and
SOCKS gateways, as configured in the user’s system preferences. You
can also add support for your own custom networking protocols and URL
schemes (for your app’s private use).
没有sftp计划的支持.