请帮忙使用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()
Run Code Online (Sandbox Code Playgroud)
// 第二种方法
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()
}
Run Code Online (Sandbox Code Playgroud)
// 第三种方法
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()
}
Run Code Online (Sandbox Code Playgroud)
来自NSURLSession文档:
\n\n\nNSURLSession 类本身支持数据、文件、ftp、http、\n 和 https URL 方案,并对代理服务器和\n SOCKS 网关提供透明支持,如 user\xe2\x80\x99s 系统首选项中配置的那样。您还可以添加对您自己的自定义网络协议和 URL 方案的支持(供您的应用程序\xe2\x80\x99s 私人使用)。
\n
没有对sftp方案的支持。
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |