在Swift中使用Alamofire阻止重定向响应

编程入门 行业动态 更新时间:2024-10-26 05:34:22
本文介绍了在Swift中使用Alamofire阻止重定向响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找示例代码,该示例代码如何在请求网络API时防止重定向响应(状态代码3xx)。我在Alamofire 1.2中使用Swift。 我尝试过:

I'm looking for example code how to prevent redirect response (status code 3xx) when request web api. I'm using Swift with Alamofire 1.2. I have tried:

delegate.taskWillPerformHTTPRedirection = { (session: NSURLSession!, task: NSURLSessionTask!, response: NSHTTPURLResponse!, request: NSURLRequest!) in return nil }

不起作用

我也尝试过: github/Alamofire/Alamofire/pull/350/files ,并将自己的代码更改为:

I've also tried: github/Alamofire/Alamofire/pull/350/files and have changed my own code to:

var acc = self.txtAccount.text var pwd = self.txtPassword.text var url : String = "10.1.0.2:8081/wordpress/wp-json/users/me" let delegate = Alamofire.Manager.sharedInstance.delegate delegate.taskWillPerformHTTPRedirection = { (session: NSURLSession!, task: NSURLSessionTask!, response: NSHTTPURLResponse!, request: NSURLRequest!) in var request = NSMutableURLRequest(URL: NSURL(string: url)!) request.HTTPMethod = "GET" var credential = "\(acc):\(pwd)" var authData = credential.dataUsingEncoding(NSUTF8StringEncoding) var encodedAuthData = authData?.base64EncodedStringWithOptions(nil) var authValue = "Basic \(encodedAuthData!)" request.setValue(authValue, forHTTPHeaderField: "Authorization") request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("application/json", forHTTPHeaderField: "Accept") return request } //I've implemented URLRequestConvertible 'Router'. it also have call the same above url Alamofire.request(Router.Authorize(acc, pwd)) .response({(request, response, data, error) in println(request) })

但是它没有用,似乎变成了无限循环。我对查尔斯进行了测试。

But it's not worked and seem like turned to infinite loop. I tested on Charles.

推荐答案

使用AlamoFire 2.4(Xcode7)的替代(代码段)解决方案。就我而言,我一直期望重定向。 (我正在解压缩缩短的链接。)如果request.response调用中的完成运行,这对我来说是错误的。

Alternative (code snippet) solution using AlamoFire 2.4 (Xcode7). In my case, I always expect a redirect. (I am unpacking a shortened link.) If the completion in the request.response call runs, that is an error to me.

func printRedirectUrl() { // taskWillPerformHTTPRedirectionWithCompletion: ((NSURLSession, NSURLSessionTask, NSHTTPURLResponse, NSURLRequest, NSURLRequest? -> Void) -> Void)? Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirectionWithCompletion = { session, task, response, request, completion in // request.URL has the redirected URL inside of it, no need to parse the body print("REDIRECT Request: \(request)") if let url = request.URL { print("Extracted URL: \(url)") } Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirection = nil // Restore redirect abilities return } // We expect a redirect, so the completion of this call should never execute let url = NSURL(string: "google") let request = Alamofire.request(.GET, url!) request.response { request, response, data, error in print("Logic Error, response should NOT have been called for request: \(request)") Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirection = nil // Restore redirect abilities - just in case } }

重定向请求:{URL: www.google/ }

提取的网址: www.google/

更多推荐

在Swift中使用Alamofire阻止重定向响应

本文发布于:2023-11-26 01:59:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Swift   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!