2016-11-18 85 views
1

我通過使用POST請求使用URLRequestURLSession通過API登錄。自從我收到來自API的正確響應後,一切似乎都運行良好。在HTTP請求後保存併發送cookie

然後我想發送一個GET請求到相同的API,但似乎我沒有連接了。

對於我所看到的,API在其頭以該形式發送一個Cookie:

Cookie:PHPSESSID=abc123

我想這個問題是從餅乾至極來當我做一個GET請求不發送。

這是我的代碼。

的ViewController:

@IBAction func Connection(_ sender: AnyObject) { 
    let loginFunc = Login() 
    loginFunc.login(username: username.text!, password: password.text!) { jsonString in 
      let response = jsonString 
      print(response) 
    } 
    let get = GetRequest() 
    get.get(req: "patient/info") { jsonString in 
     let response = jsonString 
     print(response) 
    } 
} 

Login.swift:

class Login { 
    func login(username: String, password: String, completion: @escaping (String) ->()) { 
     var request = URLRequest(url: URL(string: "http://myurl/web/app_dev.php/login_check")!) 
     request.httpMethod = "POST" 
     let config = URLSessionConfiguration.default 
     let postString = "_username=" + username + "&_password=" + password 
     request.httpBody = postString.data(using: .utf8) 
     var responseString = "" 
     let mysession = URLSession.init(configuration: config) 
     let task = mysession.dataTask(with: request) { data, response, error in 
      guard let data = data, error == nil else {             // check for fundamental networking error 
       print("error=\(error)") 
       return 
      } 

      if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
       print("statusCode should be 200, but is \(httpStatus.statusCode)") 
       print("response = \(response)") 
      } 

      responseString = String(data: data, encoding: .utf8)! 
      let httpResponse = response as! HTTPURLResponse 
      let field = httpResponse.allHeaderFields["Cookie"] 
      print(field) 
      print(type(of: response)) 
      completion(responseString) 
     } 
     task.resume() 
    } 
    } 

GetRequest.swift:

class GetRequest { 
    func get(req: String, completion: @escaping (String) ->()) { 
     var request = URLRequest(url: URL(string: "http://myurl/web/app_dev.php/" + req)!) 
     request.httpMethod = "GET" 
     var responseString = "" 
     let task = URLSession.shared.dataTask(with: request) { data, response, error in 
      guard let data = data, error == nil else {             // check for fundamental networking error 
       print("error=\(error)") 
       return 
      } 

      if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
       print("statusCode should be 200, but is \(httpStatus.statusCode)") 
       print("response = \(response)") 
      } 

      responseString = String(data: data, encoding: .utf8)! 
      print(responseString) 
      completion(responseString) 
     } 
    task.resume() 
    } 
} 
+0

如果你的應用程序終止,會話cookie將不會保存,因爲它是一個會話cookie –

+0

你是什麼意思?它不終止,我發送獲取請求後 – ZeenaZeek

+0

奇怪,請嘗試此會話配置呢? let config = URLSessionConfiguration.default config.httpCookieAcceptPolicy = .always –

回答

-1

嘿與您的Ajax請求,請使用一起發送的cookie。你需要使用withCredentials爲true。

$.ajaxSetup({ 
    dataType: 'json', 
    contentType: 'application/json', 
    async: true, 
    cache: false, 
    processData: false, 
    crossDomain: true, 
    xhrFields: { 
    withCredentials: true 
    } 
}); 
+1

這是swift3不是ajax? – ZeenaZeek

+0

哎,對不起,你可以使用相同的概念。 :) –