2017-08-28 40 views
1

我试图做一个soap调用加密函数并返回一个值的请求。当我对邮递员提出要求时,它就会起作用。但是,我从邮递员那里得到了代码,但它返回错误代码500.我认为问题可以在postData中,我在双引号之前加\。 代码:错误代码500与Swift中的肥皂请求

 let headers = [ 
     "content-type": "text/xml", 
     "cache-control": "no-cache", 
     "postman-token": "4d266611-ea55-0c8a-7879-eac8bf387ed0" 
    ] 

    let postData = NSData(data: "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><Encrypt xmlns=\"http://tempuri.org/\"><request>RequestIsValid29:01:2015 16:31</request> </Encrypt></soap:Body> </soap:Envelope>".data(using: String.Encoding.utf8)!) 

    let request = NSMutableURLRequest(url: NSURL(string: "http://mobileexam.veripark.com/mobileforeks/service.asmx")! as URL, 
             cachePolicy: .useProtocolCachePolicy, 
             timeoutInterval: 10.0) 
    request.httpMethod = "POST" 
    request.allHTTPHeaderFields = headers 
    request.httpBody = postData as Data 

    let session = URLSession.shared 
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in 
     if (error != nil) { 
      print(error) 
     } else { 
      let httpResponse = response as? HTTPURLResponse 
      print(httpResponse) 
     } 
    }) 

    dataTask.resume() 

错误代码:

<NSHTTPURLResponse: 0x608000222300> { URL: http://mobileexam.veripark.com/mobileforeks/service.asmx } { status code: 500, headers { 
    "Cache-Control" = private; 
    Connection = "Keep-Alive"; 
    "Content-Length" = 1802; 
    "Content-Type" = "application/soap+xml; charset=utf-8"; 
    Date = "Mon, 28 Aug 2017 14:57:47 GMT"; 
    Server = "Microsoft-IIS/7.5"; 
    "X-AspNet-Version" = "4.0.30319"; 
    "X-Powered-By" = "ASP.NET"; 
} }) 
+0

设置URLRequest的“内容类型”:https://stackoverflow.com/questions/2965587/valid-content-typ e-for-xml-html-and-xhtml-documents? – Larme

+0

我更改了代码,请检查一下吗? –

+0

@iOSDeveloper是的。确切的问题是什么? –

回答

1

你试试?

[lobj_Request setValue:@“gzip”forHTTPHeaderField:@“Accept-Encoding”];

+0

我改变了这个问题,你能检查一下吗? –

+0

嗯,我只是试图把http://mobileexam.veripark.com/mobileforeks/service.asmx放到我的网页浏览器中,看起来有一些错误。你的网址是否正确? – user3797599

+0

是的。我用邮差尝试它,并成功返回。我认为问题是在我的postdata中,我在\“ –

2

正如我在问题中提到的那样。问题是XML中的字符串部分。我前添加\“,也有\ r当需要新的生产线下面的部分是XML的右弦版本

。‘\ rhttp://www.w3.org/2001/XMLSchema-instance \’的xmlns:XSD = \ “http://www.w3.org/2001/XMLSchema\ ”的xmlns:SOAP = \“ http://schemas.xmlsoap.org/soap/envelope/\ ”> \ r \ rhttp://tempuri.org/ \“> \ rRequestIsValid29:01:2015 16点31 \ r \ R”

因此,对于问题的最后工作代码为:

let poststring = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r<soapenv:Header/> <soapenv:Body>\r<tem:GetForexStocksandIndexesInfo> <tem:request>\r<tem:IsIPAD>true</tem:IsIPAD>\r<tem:DeviceID>test</tem:DeviceID>\r<tem:DeviceType>ipad</tem:DeviceType>\r<tem:RequestKey>\(self.encrypt_code!)</tem:RequestKey>\r<tem:RequestedSymbol>\(name)</tem:RequestedSymbol>\r<tem:Period>Month</tem:Period> </tem:request>\r</tem:GetForexStocksandIndexesInfo> </soapenv:Body>\r</soapenv:Envelope>" 
let postData = NSData(data: poststring.data(using: String.Encoding.utf8)!) 
let request = NSMutableURLRequest(url: NSURL(string: "http://mobileexam.veripark.com/mobileforeks/service.asmx")! as URL, 
             cachePolicy: .useProtocolCachePolicy, 
             timeoutInterval: 10.0)