错误:错误域= HTTPTask代码= 404“找不到页面";UserInfo = 0x608000228720 {NSLocalizedDescription =页面未找到} [英] error: Error Domain=HTTPTask Code=404 "page not found" UserInfo=0x608000228720 {NSLocalizedDescription=page not found}

查看:47
本文介绍了错误:错误域= HTTPTask代码= 404“找不到页面";UserInfo = 0x608000228720 {NSLocalizedDescription =页面未找到}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift编程语言的新手.我正在构建Mac应用程序.我必须通过将httprequest发送到我的api来与我的api集成.

As I am new for the Swift Programming Language. I am building the mac app. I have to integrate with my api by sending the httprequest to my api.

我正在将SwiftHTTP子模块用于http请求.

I am using the submodule SwiftHTTP for the http request.

这是我的代码:

import Cocoa

import SwiftHTTPOSX

class ViewController: NSViewController {

@IBOutlet weak var EmailField: NSTextField!

@IBOutlet weak var PasswordField: NSSecureTextField!

@IBAction func signin(sender: AnyObject) {
    println("email is \(EmailField.objectValue) and Password is \(PasswordField.objectValue)")

    var request = HTTPTask()
    request.GET("https:user@gmail.com:password@api.domain.co/api/v3/auth/token/", parameters: nil, success: {(response: HTTPResponse) in
        if let data = response.responseObject as? NSData {
            let str = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("response: \(response.responseObject)")
        }
        }, failure: {(error: NSError, response: HTTPResponse?) in
            println("error: \(error)")
    })

}
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override var representedObject: AnyObject? {
    didSet {
    // Update the view, if already loaded.
    }
}

}

错误:

error: Error Domain=HTTPTask Code=401 "accessed denied" UserInfo=0x60000002ae20 {NSLocalizedDescription=accessed denied}

预期的输出描述:

当我向"http:user@gmail.com:password@api.domain.co/api/v3/auth/token"发送请求时,我将获得apikey.但是上面的错误使我丧命.

When I send request to the "http:user@gmail.com:password@api.domain.co/api/v3/auth/token", in the response I will get the apikey. But the above error killing me.

推荐答案

完全不需要使用SwiftHTTP.而且您还使用 user@email.com:password@api.domain/com/....格式,该格式在您使用Javascript或Jquery时有效.

No need of using the SwiftHTTP at all. And also you are using the format user@email.com:password@api.domain/com/.... which works while you are working in Javascript or Jquery.

在Swift中,它不接受basic-auth.对于基本身份验证,我们必须将编码字符串发送到身份验证网址,例如:

In Swift, it doesn't takes in the basic-auth. For the basic auth, we have to send with the encode string to the authentication url like:

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let userPasswordString = "\(Email.stringValue):\(Password.stringValue)"
    let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
    let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(nil)
    let authString = "Basic \(base64EncodedCredential)"
    println("\(authString)")
    config.HTTPAdditionalHeaders = ["Authorization" : authString]
    let session = NSURLSession(configuration: config)



    let url = NSURL(string: "https://api.domain.com/api/v2/auth/token/")
    let task = session.dataTaskWithURL(url!) {
        (let data, let response, let error) in
        if let httpResponse = response as? NSHTTPURLResponse {
            let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
}
}

之后,只需打印数据,看它一定会满足您的问题.

After then just print the data and see it will definitely meets your question.

这篇关于错误:错误域= HTTPTask代码= 404“找不到页面";UserInfo = 0x608000228720 {NSLocalizedDescription =页面未找到}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆