从Swift 2.0调用WCF(在Azure上)失败,状态码为400
问题描述:
尝试调用托管在Azure上的WCF服务,失败并出现HTTP错误400. Web服务本质上充当我的Braintree(移动支付)服务器事物的一面。从Swift 2.0调用WCF(在Azure上)失败,状态码为400
下面是相关代码:发生在
let soapEnvelope = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Header><Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IService1/GenerateToken</Action></s:Header><s:Body><GenerateToken xmlns=\"http://tempuri.org/\" /></s:Body></s:Envelope>"
let soapEnvelopeLength = String(soapEnvelope.characters.count)
let clientTokenURL = NSURL(string: "http://xxx.xxx.net/Service1.svc")
let clientTokenRequest = NSMutableURLRequest(URL: clientTokenURL!)
let session = NSURLSession.sharedSession()
clientTokenRequest.HTTPMethod = "POST"
clientTokenRequest.HTTPBody = soapEnvelope.dataUsingEncoding(NSUTF8StringEncoding)
clientTokenRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
clientTokenRequest.addValue(soapEnvelopeLength, forHTTPHeaderField: "Content-Length")
clientTokenRequest.addValue("http://tempuri.org/IService1/GenerateToken", forHTTPHeaderField: "soapAction")
let task = session.dataTaskWithRequest(clientTokenRequest, completionHandler: {data, response, error -> Void in
print(response)
let clientToken = String(data: data!, encoding: NSUTF8StringEncoding)
let brainTree = Braintree(clientToken: clientToken!)
if error != nil {
print(error)
}
})
task.resume()
失败:
let task = session.dataTaskWithRequest(clientTokenRequest, completionHandler: {data, response, error -> Void in
我的SoapEnvelope几乎是从WCF测试客户端工具自动生成的XML的复制/粘贴。
我也试着用下面的SoapEnvelope,但仍得到一个400。结果clientToken保持零和B树永远不会初始化:
let soapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><GenerateToken xmlns=\"http://tempuri.org/\" /></s:Body></s:Envelope>"
任何想法?
感谢, 城邦
答
监测与小提琴手交通后回答我的问题,我不得不SOAP信封改为:
let soapEnvelope = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><GenerateToken xmlns=\"http://tempuri.org/\" /></s:Body></s:Envelope>"
这个工作。