2017-02-08 17 views
-1

StackOverflowで他の質問と回答を確認してみても、次の問題が引き続き発生します。URLSessionリクエストはサーバーで受け付けなくなりました

このアプリは、ユーザーのユーザー名とパスワードを確認し、ユーザーとパスワードが有効であることをサーバーが認証した場合に別の画面でサーバーにログオンすることを目的としています。

JSONレスポンスは、キー成功を持っていることを意図していると、それは[OK]ををだ場合、ユーザーがログオンできない他場合にログに記録します。

Swift 2でうまく動作し、Swift 2から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のシリアル化は正常に機能していますが、Successキーがないため、サーバーに正しいURLデータを与えていません。

そしてSwift2とAndroid版とWindows版でサーバーが正常に動作していることを確認しましたので、私のコードだと分かりました。

答えて

0

次のコードを試してください。これは私の最後でうまくいきます。 Plsチェック:

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

関連する問題