我想向此网址发送请求: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()
}