Search code examples
swiftcookiesios9nsurlsessionnsurlrequest

How to POST with cookies by NSURLSession


I want to POST with cookies to pass the log in system of my school, and I have done that by Python. However, I don't know how to do that with NSURLSession. I have tried to turn the cookies into a string and use the NSURLRequest.setValue, but it didn't work to me. Or I have also tried

let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(fields, forURL: response!.URL!)
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies)
request.allHTTPHeaderFields = headers

But it didn't work either. I have searched online try to figure it out, but the answers are too old to solve the problem here. So please somebody can help me out with this?


Solution

  • Unless you're customizing the NSURLSession instance via NSURLSessionConfiguration, you should just be able to set the cookie via the shared NSHTTPCookieStorage using the setCookie selector.