gpt4 book ai didi

ios - 在重定向之前停止 NSURL 连接,同时获取重定向 URL

转载 作者:搜寻专家 更新时间:2023-10-31 08:33:47 24 4
gpt4 key购买 nike

我试图取回一个 url 的响应正文,它重定向到另一个无效的 url。我也想停止重定向,因为它会使我的应用程序崩溃。

我已经尝试在我的代码中使用以下方法,但没有被采纳:

func URLSession(_ session: NSURLSession,
dataTask dataTask: NSURLSessionDataTask,
didReceiveResponse response: NSURLResponse,
completionHandler completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("Did you get here?")
}

func URLSession(_ session: NSURLSession,
task task: NSURLSessionTask,
didCompleteWithError error: NSError?) {
print("How about here?")
}

func URLSession(_ session: NSURLSession,
task task: NSURLSessionTask,
willPerformHTTPRedirection response: NSHTTPURLResponse,
newRequest request: NSURLRequest,
completionHandler completionHandler: (NSURLRequest?) -> Void) {
print("Maybe here?")
}

func URLSession(_ session: NSURLSession,
didBecomeInvalidWithError error: NSError?) {
print("Did you find an error?")
}

对我能做什么有什么想法吗?

编辑:所以,这是我拥有的更新代码,使用 XCode 7 时不会出现任何错误:

class Example: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {


override init() {
super.init()

let mySession = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())

// Put your handler as the second parameter.
let data = try!mySession.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: myHandler)

data!.resume()
}

// Create your handler with the following signature, and read the response body.
func myHandler(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void {

// In this case the “encoding” is NSASCIIStringEnconding. It depends on the website.
let responseBody = NSString(data: data!, encoding: NSASCIIStringEncoding)

print(responseBody)
}



// Handles redirection
private func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {

// Stops the redirection, and returns (internally) the response body.
completionHandler(nil)
}

最佳答案

要停止重定向并获取响应主体,您应该使用 nil 作为参数调用 completionHandler

停止重定向后,当您使用 dataTaskWithURL:completionHandler: 创建 HTTP Get 请求时,URLSession 将调用您指定的处理程序(也可以是 dataTaskWithRequest:completionHandler:).然后在处理程序中你得到响应体。示例:

import Foundation

class Example: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {


override init() {
super.init()

let mySession = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())

// Put your handler as the second parameter.
let data = mySession.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: myHandler)

data.resume()
}

// Create your handler with the following signature, and read the response body.
func myHandler(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void {

// In this case the “encoding” is NSASCIIStringEnconding. It depends on the website.
let responseBody = NSString(data: data, encoding: NSASCIIStringEncoding)

println(responseBody)
}



// Handles redirection
func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {

// Stops the redirection, and returns (internally) the response body.
completionHandler(nil)
}
}

Apple 引用号:https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionTaskDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionTaskDelegate/URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler :

我知道它适用于 HTTP 302 代码,但对于 301(永久移动)或 308 等其他代码我不知道它是否适用。

值得添加(关于URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:):

This method is called only for tasks in default and ephemeral sessions. Tasks in background sessions automatically follow redirects.

关于ios - 在重定向之前停止 NSURL 连接,同时获取重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31592286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com