private func httpPost(requestURL:String,poststring:String) -> [String : AnyObject] { return httpSync(requestURL: requestURL,poststring: poststring,method: "POST"); } private func httpGet(requestURL:String)->[String : AnyObject]{ return httpSync(requestURL: requestURL,poststring: "",method: "GET"); } private func httpSync(requestURL:String,poststring:String,method:String)->[String : AnyObject]{ var request = URLRequest(url: URL(string:requestURL)!) request.setValue("application/x-www-form-urlencoded; charset=utf-8",forhttpheaderFIEld: "Content-Type") request.setValue("application/Json; charset=utf-8",forhttpheaderFIEld: "Accept") if(httpMeta.Token != ""){ request.setValue("BEARER " + httpMeta.Token,forhttpheaderFIEld: "Authorization"); } request.httpMethod = method request.httpBody = poststring.data(using: String.EnCoding.utf8); // print(request.deBUGDescription) var result:[String:AnyObject] = [:]; let semaphore = dispatchSemaphore(value:0) let task = URLSession.shared.dataTask(with: request) { data,response,error in guard let data = data,error == nil else { //print(error ?? <#default value#>) return } do { print(data.deBUGDescription) print(response.deBUGDescription) let responSEObject = try JsONSerialization.JsonObject(with: data,options: JsONSerialization.ReadingOptions.allowFragments) result = (responSEObject as? [String : AnyObject])!; print(result) // print(responSEObject) } catch let JsonError { print(JsonError.localizedDescription) // print("Json error: \(JsonError.localizedDescription)") } semaphore.signal() } task.resume() _ = semaphore.wait(timeout: .distantFuture) return result }总结
以上是内存溢出为你收集整理的swift 3 发送 HTTP 请求函数全部内容,希望文章能够帮你解决swift 3 发送 HTTP 请求函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)