0

Alamofire を介して POST NSMutableURLRequest リクエストを実行しようとしていますが、これらのフィールド値を HTTP ヘッダーに設定できません。助けはありますか?

    var params = ["username":"veeru@gamil.com", "password":"Pasword123!", "csrfmiddlewaretoken":csrfToken] as Dictionary<String, String>

    var post:NSString = "csrfmiddlewaretoken=Basic \(csrfToken)&username=veeru@gmail.com&password=Password123!"
    var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

            var URL = NSURL(string: "https://myurl/account/login")
            var mutableURLRequest = NSMutableURLRequest(URL: URL!)
            mutableURLRequest.setValue("Content-Type", forHTTPHeaderField: "application/x-www-form-urlencoded")
            mutableURLRequest.setValue("keep-alive", forHTTPHeaderField: "Connection")
            mutableURLRequest.setValue("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36", forHTTPHeaderField: "User-Agent")
            mutableURLRequest.setValue("Code", forHTTPHeaderField: "X-CSRFToken")
                           mutableURLRequest.HTTPMethod = "POST"
            mutableURLRequest.HTTPBody = postData
            let username = "veeru@gmail.com"
            let password = "Password123!"

            let loginString = NSString(format: "%@:%@", username, password)
            let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
            let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
            mutableURLRequest.setValue(base64LoginString, forHTTPHeaderField: "Authorization")
            let manager = Alamofire.Manager.sharedInstance // or create a new one
            let request = manager.request(mutableURLRequest)
            Alamofire.request(.POST, "https://url/account/login",parameters: params)
                    .responseString{ (request, response, data, error) in
                        println("Request :\(request)\n\n")
                        println("Response: \(response)\n\n")
                        println("Data\(data)")
                        println("Error : \(error)")
            println("Done")
            }
4

1 に答える 1

0

実際にオブジェクトAlamofire.request(.POST, "https://url/account/login",parameters: params)を作成するためにすべてのトラブルを経験したのに、なぜ行があるのですか?NSURLRequest

それを削除するだけです:

let request = manager.request(mutableURLRequest)
request.responseString{ (request, response, string, error) in
  // ...
}
于 2014-11-19T20:54:47.093 に答える