gpt4 book ai didi

ios - 如何使用 iOS Swift 在 REST Web 服务 URL 上应用 Cookie

转载 作者:行者123 更新时间:2023-11-30 14:18:21 25 4
gpt4 key购买 nike

我已经从我的应用程序发出了 REST 请求,并且我能够正确获取数据。现在我想在此 Web 服务 URL 上应用身份验证 cookie。我们应该怎么做。请帮忙。下面是我当前的代码。

CallRestParser.swift - 在此 processRequest 方法将从某个 View Controller 调用。 processRequest 方法将接收 Web 服务 URL 并向 RESTParser 类发送请求。 getReceiveData 是一个委托(delegate)方法,连接成功后会调用该方法。

protocol UpdateProjectUIDelegate{
func UpdateProjectUI(data:NSDictionary)
}
class CallRESTParser: NSObject, RESTParserDelegate {
var rest1=RESTParser()
var projectDelegate: UpdateProjectUIDelegate?
func processRequest(url:String) {
var nsURL = NSURL(string: url)
var createrequest: NSMutableURLRequest = NSMutableURLRequest(URL: nsURL!)
createrequest.HTTPMethod = "GET"
rest1.delegate = self
rest1.httpRequest(createrequest)

}

func getReceiveData(recvData:NSMutableData,sender:RESTParser){
var error: NSError?

var jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(recvData, options:NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary

if error != nil{
print("Error occured \(error?.localizedFailureReason)")
return
}
println(jsonResult.count)
projectDelegate?.UpdateProjectUI(jsonResult)
//var updateUI = ProjectDetails_tab()
//updateUI.updateUI()


}
}

RESTParser.swift -

protocol RESTParserDelegate{
func getReceiveData(data:NSMutableData,sender:RESTParser)
}


class RESTParser: NSObject, NSURLConnectionDataDelegate {

var receiveData: NSMutableData!
var requestConnection: NSURLConnection!
var delegate: RESTParserDelegate?

func receiveData(resData:NSMutableData){
receiveData = resData
}

func requestConnection(reqConn:NSURLConnection){
requestConnection = reqConn
}

func httpRequest(myRequest:NSMutableURLRequest){
self.requestConnection = NSURLConnection(request: myRequest, delegate: self)

}
// NSURLConnectionDataDelegate methods
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
self.receiveData = NSMutableData()

}

func connection(connection: NSURLConnection, didReceiveData data: NSData){
self.receiveData.appendData(data)
}

func connectionDidFinishLoading(connection: NSURLConnection){
//if let revData = receiveData{
self.delegate?.getReceiveData(receiveData, sender: self)
//}else
//{
// NSLog("No data : nil")
//}
self.delegate = nil
self.receiveData = nil
self.requestConnection = nil
}
func connection(connection: NSURLConnection, didFailWithError error: NSError){
NSLog("Failed with error - %@ ",error.localizedDescription)

}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
return protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust){
if(challenge.protectionSpace.host == "domain.com"){
let credentials = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
challenge.sender.useCredential(credentials, forAuthenticationChallenge: challenge)
}
}
challenge.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)

}
}

我们从 authSessionCookieKeyVal 获得的身份验证 Cookie 是这样的 - XX-X-SESSION-ID=1_2_1_XXxXXXgSaG1K5aXXXXXXXV 。我如何在上述 REST 调用中应用此 cookie。请帮忙。

最佳答案

您必须在NSMutableURLRequestheader中传递cookie

    var nsURL = NSURL(string: url)
var createrequest: NSMutableURLRequest = NSMutableURLRequest(URL: nsURL!)

let cookieArray = NSArray(objects: Cookie1,Cookie2) // Cookie1 and Cookie2 is an object of NSHTTPCookie
let cookieHeader = NSHTTPCookie.requestHeaderFieldsWithCookies(cookieArray)
createrequest.allHTTPHeaderFields = NSDictionary(dictionary: cookieHeader)

编辑

Cookie 存储在 NSHTTPCookieStorage

    var Cookie1:NSHTTPCookie?  // Cookie Object
var Cookie2:NSHTTPCookie? // Cookie Object
let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
let cookieArray = storage.cookies!

for tempCookie in cookieArray
{
if tempCookie.name == "rtFa" // Write your cookie name which you want to search
{
Cookie1 = tempCookie as? NSHTTPCookie
continue
}
else if tempCookie.name == "FedAuth" // Write your cookie name which you want to search
{
Cookie2 = tempCookie as? NSHTTPCookie
continue
}
}

if Cookie1 != nil && Cookie2 != nil
{
println("Cookie Found")
}
else
{
println("Cookie Not Found")
}

关于ios - 如何使用 iOS Swift 在 REST Web 服务 URL 上应用 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30840085/

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