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 请求函数的更多相关文章

  1. C++发送HTTP请求---亲测可行(转)

    转自:http://hi.baidu.com/benbearlove/item/1671c23017575825b3c0c53f 环境:xp sp3,vs2008,在静态库中使用 MFC #inclu ...

  2. 发送curl请求的函数

    //发送curl请求的函数function curl_request($url, $post = false, $data=array(), $https = false){ //使用curl_ini ...

  3. pythone函数基础(13)发送网络请求

    需要导入urllib模块,request模块发送网络请求有两种方法 第一种方法# from urllib.request import urlopen# from urllib.parse impor ...

  4. 在发送ajax请求时加时间戳或者随机数去除js缓存

    在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...

  5. 怎样防止重复发送 Ajax 请求?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:长天之云链接:http://www.zhihu.com/question/19805411/answer/15465427来源 ...

  6. 转:jquery向普通aspx页面发送ajax请求

    本文将介绍在ASP.NET中如何方便使用Ajax,第一种当然是使用jQuery的ajax,功能强大而且操作简单方便,第二种是使用.NET封装好的ScriptManager. $.ajax向普通页面发送 ...

  7. jQuery发送ajax请求

    利用jquery发送ajax请求的几个模板代码. $.ajax({ async : false, type: 'POST', dataType : "json", url: &qu ...

  8. PHP模拟发送POST请求之五curl基本使用和多线程优化

    今天来介绍PHP模拟发送POST请求的重型武器——cURL函数库的使用和其多线程的优化方法. 说起cURL函数,可谓是老生常谈,但网上许多资料都在关键部分语焉不详,列出一大堆手册上的东西,搞得我入门时 ...

  9. PHP模拟发送POST请求之四、加强file_get_contents()发送POST请求

    使用了笨重fsockopen()方法后,我们开始在PHP函数库里寻找更简单的方式来进行POST请求,这时,我们发现了PHP的文件函数也具有与远程URL交互的功能. 最简单的是fopen()和fread ...

随机推荐

  1. C++DFS方法全排列

    前几天看纪磊的<啊哈!算法>一书,里面讲算法讲的特别通俗细致,真的是初中生都能读得懂的算法书(我大二才读:P).这段代码很适合初学算法的同学. #include<iostream&g ...

  2. mysql的一些基本知识

    一.数据类型: 字符型 整型 浮点型 日期时间型 二.数据表操作: 插入记录:INSERT  表名(···,···,···) VALUES('···','···',···): 查找记录:SELECT ...

  3. Flask-最简单的Python http服务框架使用

    环境准备 Python + pip + Flask sudo easy_install pip sudo pip install flask 代码如下(做了个jieba分词的服务) # encodin ...

  4. Pandas 高级应用 数据分析

    深入pandas 数据处理 三个阶段 数据准备 数据转化 数据聚合 数据准备 加载 组装 合并 - pandas.merge() 拼接 - pandas.concat() 组合 - pandas.Da ...

  5. 从0开始 Java学习 packet用法

    packet 包的用法 参考博客:https://www.cnblogs.com/Ring1981/p/6240412.html 用法 java 源文件带有包名,往往容易出错 如:H:\code\He ...

  6. Android自定义圆形ProgressBar

    闲来无事做了一个自定义的进度条,大致效果图如下: progressbar.gif 废话不多说,下面直接上代码: 自定义控件代码CircleProgressBar.java: public class ...

  7. Java String类为什么不可变?

    原文地址:# Why String is immutable in Java? 众所周知,String类在Java中是不可变的.不可变类简单地说是实例不可修改的类.对于一个实例创建后,其初始化的时候所 ...

  8. 代码题 — 剑指offer题目、总结

    剑指offer题目总结:  https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...

  9. SPOJ-CLEANRBT-状压dp

    CLEANRBT - Cleaning Robot #dynamic-programming #bfs Here, we want to solve path planning for a mobil ...

  10. MongoDB.Driver 2.4以上版本 在.NET中的基本操作

    MongoDB.Driver是操作mongo数据库的驱动,最近2.0以下版本已经从GitHub和Nuget中移除了,也就是说.NET Framework4.0不再能从官方获取到MongoDB的驱动了, ...