請幫助使用SFTP連接到我的服務器,我嘗試了很多。但它沒有正常工作。服務器工作正常。使用NSURLSession不工作的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」 在瀏覽器,我得到這樣的登錄表單。
如果您的網址不是「SFTP://sftp.myserver.com」,以便它可以計算出使用的協議? –
如果你想隱藏你的服務器地址,你失敗了。 –