使用 NSURLSession 的 SFTP 不起作用

Alv*_*ese 5 ios swift

请帮忙使用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)

当我在浏览器中加载此“sftp.myserver.com”时,我得到这样的登录表单。在此输入图像描述

Joh*_*cid 2

来自NSURLSession文档:

\n\n
\n

NSURLSession 类本身支持数据、文件、ftp、http、\n 和 https URL 方案,并对代理服务器和\n SOCKS 网关提供透明支持,如 user\xe2\x80\x99s 系统首选项中配置的那样。您还可以添加对您自己的自定义网络协议和 URL 方案的支持(供您的应用程序\xe2\x80\x99s 私人使用)。

\n
\n\n

没有对sftp方案的支持。

\n