2017-02-08 89 views
-1

在對StackOverflow進行審查並嘗試其他問題和解答後,我仍然遇到以下問題。服務器不再接受URLSession請求

該應用程序旨在驗證用戶的用戶名和密碼,然後將其登錄到另一個屏幕上的服務器上,前提是服務器驗證用戶和密碼是有效的。

JSON響應是爲了有一個關鍵成功,如果它是一個然後確定以登錄否則,如果用戶無法登錄。

在Swift 2中工作良好,我執行了從Swift2移動到Swift 3的建議更改,並且沒有錯誤,但對以下代碼有一個奇怪的響應。

  let body : String = ("username=\(tkUserName.text!)&password=\(tkPassword.text!)") 
     var request = NSMutableURLRequest() 
     request = NSMutableURLRequest(url: URL(string: "https://xxxxxxxxxxxx/LoginApp")!) 
     request.httpMethod = "POST" 
     request.httpBody = body.data(using: String.Encoding.utf8) 

     URLSession.shared.dataTask(with: request as URLRequest) 
     { (data, response, error) -> Void in 
      DispatchQueue.main.async(execute: {() -> Void in 

        if response == nil 
        { 
         //advise user no internet or other 
        } 
        else 
        { 
         var success : Int? 
         do { 
          let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary 
          success = jsonResult!.object(forKey: "success") as? Int 

         } 
         catch let error as NSError 
         { 
          //action error 
         } 

         if success == 1 
         { 
          //capture all is good and go to other page         
          self.performSegue(withIdentifier: "unwindThenGoToTeamPage", sender: self) 
         } 
         else 
         { 
          //THIS IS WHERE IT DROPS INTO EACH TIME AS SUCCESS WAS "0" 

         } 
        } 

      }.resume() 

這裏是服務器響應

Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 7/02/2017 10:19:31 AM 
Event time (UTC): 6/02/2017 11:19:31 PM 
Event ID: 1f9ff75ee64149b0994fa4a46a6ea03b 
Event sequence: 5 
Event occurrence: 1 
Event detail code: 0 

<NSHTTPURLResponse: 0x7b9ea440> { URL: https://xxxxxxxxxxxxx/teambeta/LoginApp } { status code: 200, headers { 
"Cache-Control" = private; 
"Content-Length" = 67; 
"Content-Type" = "application/json; charset=utf-8"; 
Date = "Wed, 08 Feb 2017 03:48:09 GMT"; 
Server = "Microsoft-IIS/7.5"; 
"Set-Cookie" = "ASP.NET_SessionId=ixxxxxxxxxxx; domain=xxxxx.com; path=/; HttpOnly, .ASPXAUTH=xxxxxxxxxxx; domain=xxxxx.com; expires=Fri, 10-Mar-2017 17:08:09 GMT; path=/; HttpOnly, TIsMobileDevice=True; path=/"; 
"X-AspNet-Version" = "4.0.30319"; 
"X-AspNetMvc-Version" = "5.2"; 
"X-Powered-By" = "ASP.NET"; 

}}

有很多多個服務器響應,但這個問題似乎是與我創建的URL會話。 JSON序列化工作正常,但沒有成功的關鍵,所以我沒有給服務器正確的URL數據。

我檢查了服務器仍然適用於Swift2以及Android和Windows版本,所以我知道這是我的代碼。

回答

0

請嘗試下面的代碼,這對我的工作很好。請檢查:

var request = URLRequest(url:url) 
    request.httpMethod = "POST" 
    request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
    request.addValue("application/json", forHTTPHeaderField: "Accept") 
    request.addValue(AUTENTICATION_KEY, forHTTPHeaderField: "AuthenticateKey") 

    request.httpBody = try! JSONSerialization.data(withJSONObject: json, options: []) 
    request.timeoutInterval = 30 
    let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in 
     guard data != nil else 
     { 
      print("no data found: \(error)") 
      return 
     } 

     do 
     { 
      let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary 
      completion(json) 
     } 
     catch let parseError 
     { 
      print(parseError) 
      // Log the error thrown by `JSONObjectWithData` 
      let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
      print("Error could not parse JSON to load staff: '\(jsonStr)'") 
     } 
    }) 
    task.resume() 
+0

由於某種原因問題被標記下來,但不知道爲什麼,將繼續努力。感謝輸入,但沒有幫助,因爲已經嘗試過了。 – timv

相關問題