ios - 使用 Swift 中更新的 header Transmission 重新发送您的请求

标签 ios swift http-post json-rpc transmission-daemon

我有这段代码来获取 X-Transmission-Session-Id,但现在我需要使用更新后的 header 重新发送请求。

function takeXTransmissionSessionId( ){

    let urls = NSURL(string: "\(url)")
    let task = NSURLSession.sharedSession().dataTaskWithURL(urls!) { (data, respornse, error) -> Void in

        guard error == nil && data != nil else {

            print("error=\(error!)")

            let alertView = UIAlertController(title: "ERROR", message: "A server with the specified hostname could not be found.", preferredStyle: UIAlertControllerStyle.Alert)
            let actionView = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
            alertView.addAction(actionView)
            self.presentViewController(alertView, animated: true, completion: nil)

            return
        }

        if let httpStatus = respornse as? NSHTTPURLResponse where httpStatus.statusCode != 200 {

            let sesionID = httpStatus.allHeaderFields["X-Transmission-Session-Id"]

            if sesionID == nil {

                let alertView = UIAlertController(title: "ERROR", message: "The request has not been applied because it lacks valid authentication credentials for the target resource.", preferredStyle: UIAlertControllerStyle.Alert)
                let actionView = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
                alertView.addAction(actionView)
                self.presentViewController(alertView, animated: true, completion: nil)

            }else{

                //print("\(respornse!)")
                //print("\(sesionID!)")
                self.XTransmissionSessionId = "\(sesionID!)"

            }

        }

    }

    task.resume()

}

我的问题是我以不同的方式多次托盘,但我不知道我需要做什么。

谢谢!

最佳答案

这是执行我需要的代码:

request.HTTPMethod = "POST"
    request.addValue("\(XTransmissionSessionId)", forHTTPHeaderField: "X-Transmission-Session-Id")

关于ios - 使用 Swift 中更新的 header Transmission 重新发送您的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863976/

相关文章:

iOS8 : running in the background for forever : GameCenter

objective-c - iOS 5 SDK 中关于使用不兼容的指针类型进行初始化的新编译器警告

ios - 无需越狱即可访问 iOS 文件系统?

ios - 如何将 Signal<Bool, NoError> 绑定(bind)到 Reactive Cocoa 4 中 UIButton 的启用属性

ios - ios 11 beta 7 的 UIKeyboardWillShowNotification 问题

ios - NSTimer 无效不起作用

swift - 将可选 block 或闭包传递给 Swift 中的函数

javascript - Angular .js :12759 : OPTIONS 405 (Method Not Allowed)

android - 在 Android 中为 POST 请求发送 XML

java - 在 POST 方法中将字符串发送到 Spring(Jackson) REST Controller