如何使用cookie进行NSURLSesssion GET请求

时间:2016-11-03 17:18:39

标签: ios swift nsurlsession

我正在使用Pinterest SDK下载Pinterest Pin的链接,(我从服务器返回的示例链接:https://www.pinterest.com/r/pin/186195765822871832/4801566892554728205/77314e40aeb26c0dc412e9cfa82f8dccc401fdb2b9806a3fe17ba8bafdb50510)。

大约5天前,当我尝试访问我从Pinterest中删除的类似链接时,我开始在我的NSURLSesssion中收到404错误。

一位朋友说,他认为Pinterest现在必须要求cookie才能访问该链接。

如何配置会话以便我可以使用Cookie并从Pinterest获得200回复​​?

更新代码:

import UIKit
import PlaygroundSupport


var url = URL(string: "https://www.pinterest.com/r/pin/186195765822871832/4801566892554728205/77314e40aeb26c0dc412e9cfa82f8dccc401fdb2b9806a3fe17ba8bafdb50510")

var getSourceURLFromPinterest: URLSessionDataTask? = nil
let sessionConfig: URLSessionConfiguration = URLSessionConfiguration.default

sessionConfig.timeoutIntervalForRequest = 30.0
sessionConfig.timeoutIntervalForResource = 30.0

let cookieJar = HTTPCookieStorage.shared
let cookieHeaderField = ["Set-Cookie": "key=value, key2=value2"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: cookieHeaderField, for: url!)
HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: url)

let session = URLSession(configuration: sessionConfig)
getSourceURLFromPinterest = session.dataTask(with: url! as URL) { (data: Data?, response: URLResponse?, error: Error?) -> Void in

        if error != nil {
        print("error is \(error)")
    }
    if response == nil {
    print("no response")

    } else if let _ = data {

        //Config Request
        let request = NSMutableURLRequest(
            url: (response?.url)!,
            cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
            timeoutInterval: 30.0)
        request.httpMethod = "HEAD"

        var statusCode = Int()
        let session = URLSession.shared
        let checkURLForResponse = session.dataTask(with: request as URLRequest) {urlData, myResponse, responseError in
            if let httpResponse = myResponse as? HTTPURLResponse {
                 statusCode = httpResponse.statusCode
                               switch statusCode {
                case  _ where statusCode < 500 && statusCode > 299 && statusCode != 405: //whitelisted 405 to exclude Amazon.com false errors
                    print("status code \(statusCode) for \(url)")
                default:
                    break;
                }
            } else {  print("***NO httpResponse for \(url)") }
        }
        checkURLForResponse.resume()
    }
}
 getSourceURLFromPinterest!.resume()

PlaygroundPage.current.needsIndefiniteExecution = true

3 个答案:

答案 0 :(得分:2)

其他答案可能会正常工作,但对我来说,这就是我编写请求的方式,以便从Pinterest的服务器获得响应。请注意,具体而言,我认为与Pinterest服务器中可能存在的错误有关,请参阅:https://github.com/pinterest/ios-pdk/issues/124

我注释了我的个人Pinterest会话ID

var cookieSession = String()
var cookieCSRFToken = String()

var myWebServiceUrl = URL(string: "https://www.pinterest.com/r/pin/186195765821344905/4801566892554728205/a9bb098fcbd6b73c4f38a127caca17491dafc57135e9bbf6a0fdd61eab4ba885")

let requestOne = URLRequest(url: myWebServiceUrl!)
let sessionOne = URLSession.shared

let taskOne = sessionOne.dataTask(with: requestOne, completionHandler: { (data, response, error) in
    if let error = error {
        print("ERROR: \(error)")
    }
    else {
        print("RESPONSE: \(response)")

        if let data = data, let dataString = String(data: data, encoding: .utf8) {
            print("DATA: " + dataString)
        } // end: if

        var cookies:[HTTPCookie] = HTTPCookieStorage.shared.cookies! as [HTTPCookie]
        print("Cookies Count = \(cookies.count)")
        for cookie:HTTPCookie in cookies as [HTTPCookie] {

            // Get the _pinterest_sess ID
            if cookie.name as String == "_pinterest_sess" {
                //var cookieValue : String = "CookieName=" + cookie.value as String
                cookieSession = cookie.value as String
                print(cookieSession)
            }

            // Get the csrftoken
            if cookie.name as String == "csrftoken" {
                cookieCSRFToken = cookie.value
                print(cookieCSRFToken)
            }
        } // end: for

    } // end: if
})
taskOne.resume()

var requestTwo = URLRequest(url: myWebServiceUrl!)

cookieSession = "XXXXXXXX"

cookieCSRFToken = "JHDylCCKKNToE4VXgofq1ad3hg06uKKl"

var cookieRequest = "_auth=1; _b=\"AR4XTkMmqo9JKradOZyuMoSWcMdsBMuBHHIM21wj2RPInwdkbl2yuy56yQR4iqxJ+N4=\"; _pinterest_pfob=disabled; _pinterest_sess=\"" + cookieSession + "\"; csrftoken=" + cookieCSRFToken as String


requestTwo.setValue(cookieRequest as String, forHTTPHeaderField: "Cookie")


let taskTwo = sessionOne.dataTask(with: requestTwo, completionHandler: { (data, response, error) in
    if let error = error {
        print("ERROR: \(error)")
    }
    else {
        print("RESPONSE: \(response)")

    } // end: if
})
taskTwo.resume()

PlaygroundPage.current.needsIndefiniteExecution = true

答案 1 :(得分:0)

您可以通过以下方式配置基于cookie的会话。如果您需要任何帮助,请告诉我。以下只是一个例子

    let session: URLSession = URLSession.shared
    session.dataTask(with: myUrlRequest { urlData, response, responseError in

        let httpRes: HTTPURLResponse = (response as? HTTPURLResponse)!
        let cookies:[HTTPCookie] = HTTPCookie.cookies(withResponseHeaderFields: httpRes.allHeaderFields as! [String : String], for: httpRes.url!)
        HTTPCookieStorage.shared.setCookies(cookies, for: response?.url!, mainDocumentURL: nil)
        if responseError == nil {

        }else {

        }
    }.resume()

随意建议编辑以使其更好。如果下面的内容不起作用,请告诉我。

答案 2 :(得分:0)

当您使用服务器进行身份验证时,服务器会提供您在响应标头中收到的cookie。您可以在cookie的共享存储中提取并设置为cookie。因此,每次拨打电话时,对于这些域名,cookie都将被共享和检查,如果有效,它将允许您进入。

let resp: HTTPURLResponse = (response as? HTTPURLResponse)!
    let cookies:[HTTPCookie] = HTTPCookie.cookies(withResponseHeaderFields: resp.allHeaderFields as! [String : String], for: resp.url!)
    HTTPCookieStorage.shared.setCookies(cookies, for: response?.url!, mainDocumentURL: nil)

在此,cookies将是一个数组,其中包含数组中的cookie。回复可能包含多个Cookie。

如果您使用第三方框架来管理Alamofire之类的HTTP请求,那么它将自行处理cookie管理。