2017-07-28 14 views
0

HTTPリクエストの送信をテストするこの機能があります。高速HTTPクライアントがリクエストを送信しない

public func test(url: URL) { 
    print("test") 
    var request = URLRequest(url: url!) 
    request.httpMethod = "GET" 
    let session = URLSession.shared 
    session.dataTask(with: request) { data, response, err in 
     print("Entered the completionHandler") 
     guard err == nil else { 
      print("error calling GET") 
      print(err!) 
      return 
     } 
    }.resume() 
} 

私のテストでコードを実行してリクエストを送信していることを確認します。 そして完了ブロックに入ることはありません(Entered the completionHandlerは決して印刷されません)。私はスウィフトに慣れていません。

func test_download() { 
    myClient.test(url: URL(string:"https://www.google.com")!) 
    print("sleeping...") 
    sleep(10) 
    print("done...") 
} 
+0

を、そのコードが動作する必要があります。残りの印刷呼び出しは機能していますか? –

答えて

0

クロージャを正しく使用していないようです。代わりにこれを試してください:あなたはURLSessionConfiguration使用してセッション上で設定する必要があるよう

// No need to explicitly set GET method since this is the default one. 
let session = URLSession.shared 
var request = URLRequest(url: url!) 
session.dataTask(with: request) { (data, response, err) in 
    print("Entered the completionHandler") 
    guard err == nil else { 
     print("error calling GET") 
     return 
    } 
    // Do whatever you need to with data and response in here 

}.resume() 
0

はルックス: - 重複強制アンラップを除き

let urlConfig = URLSessionConfiguration.default 
urlConfig.timeoutIntervalForRequest = 5 
urlConfig.timeoutIntervalForResource = 5 
let session = Foundation.URLSession(configuration: urlConfig, delegate: nil, delegateQueue: nil) 
//Now try your code 
let task = session.dataTask(with: request) { data, response, err in 
     print("Entered the completionHandler") 
     guard err == nil else { 
      print("error calling GET") 
      print(err!) 
      return 
     } 
    } 
task.resume() 
関連する問題