为什么session.DataTask返回零字节数据

时间:2016-11-11 11:17:27

标签: ios swift nsurlsession

我想向此网址发送请求:http://www.hamyanpay.ir/service/index.php,我的服务器响应

{"error":0,"result":"{\"Description\":\"\u067e\u0631\u062f\u0627\u062e\u062a \u0646\u0627\u0645\u0648\u0641\u0642\",\"RRN\":null,\"ResponseCode\":\"96\"}"}

有我的代码:

 let params = ["APP_Name" : "Avita",
                          "APP_Code" : 16,
                          "APP_Version" : "1",
                          "PAN" : "wpFUGBu8olWWIgVnVE584S0ehAUdFW3M",
                          "PIN2" : "Z7684cZ0Zk0=",
                          "CVV2" : "udUrxYSaEHQ=",
                          "ExpDate" : "2812hb9HQ=",
                          "SendSms" : false,
                          "SmsToNumber" : "",
                          "Amount" : price,
                          "ServiceID" : 101,
                          "CommandType":4,
                          "Ncode":userName]as [String : Any]
      func fetchWChargeRequest(completion: @escaping (WalletChargeResult) -> Void){
        let postData
            = try! JSONSerialization.data(withJSONObject: param, options: JSONSerialization.WritingOptions.prettyPrinted)

            let jsonString = NSString(data: postData, encoding: String.Encoding.utf8.rawValue)! as String

            self.urlstring = jsonString
            print(jsonString)
            self.urlstring = jsonString

            let headers = ["content-type":"application/json","Accept":"application/json"]

              let baseParams = [
                "action": "payment",
                "key": "123321",
                "input": jsonString ] as NSDictionary
            let jsondata
                = try! JSONSerialization.data(withJSONObject: baseParams, options: JSONSerialization.WritingOptions.prettyPrinted)
            let jsonString1 = NSString(data: jsondata, encoding: String.Encoding.utf8.rawValue)! as String
            print(jsonString1)                       
            let myUrl = NSURL(string: self.stringForUrl)

            var request = URLRequest(url: myUrl as! URL )
            request.allHTTPHeaderFields = headers
            request.httpMethod = "POST"
       request.httpBody = data1

            let urlwithPercentEscapes = paramString.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
            print(urlwithPercentEscapes)

        request.httpBody = jsondata

            let task = session.dataTask(with: request) { data, response, error in

            let httpResponse = response as? HTTPURLResponse

                if  httpResponse == response as? HTTPURLResponse {

                    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                    print("responseString = \(responseString)")
                    let statusCode = httpResponse?.statusCode
                    print(statusCode)//200
                }
                        let result = self.processWalletChargeRequest(data: data, error: error as NSError?)
            let strData = NSString(data: data!, encoding:  String.Encoding.utf8.rawValue)

                let count = data?.count
                print(count)
            print("Body: \(strData)")

            // completion closure that will be called once the web service request is completed.
            completion(result)
        }
        task.resume()
    }

为什么从session.dataTask获取的数据是零字节?发送参数有问题吗? 另外,我添加邮差输出图片: enter image description here

0 个答案:

没有答案