swift 3 发送 HTTP 请求函数

swift 3 发送 HTTP 请求函数,第1张

概述private func HttpPost(requestURL:String, postString:String) -> [String : AnyObject] { return HttpSync(requestURL: requestURL, postString: postString, method: "POST"); } private fun
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 请求函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1090196.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存