Alamofire怎么请求WebService

func reqWebService() {
let URL = NSURL(string: "http://115.29.41.12:83/serForImccoy.asmx")!
let action: String = "http://imccoy.com/webservices/isUser"
let mutableURLRequest: NSMutableURLRequest = NSMutableURLRequest(URL:URL)

let values = "{\"userId\": \"test\", \"pwd\": \"pwd\"}";
let soapMsg: String = toSoapMessage("isUser", params: "json", paramValues: values)

//mutableURLRequest.setValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
//么的,oc中,Content-Type是application/soap+xml就可以访问,swift里,就不行,必须与.asmx接口保持一致!
mutableURLRequest.setValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
mutableURLRequest.setValue(action, forHTTPHeaderField: "SOAPAction")
mutableURLRequest.setValue(String(soapMsg.length), forHTTPHeaderField: "Content-Length")
mutableURLRequest.HTTPMethod = "POST"
mutableURLRequest.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding)

var result = ""
Alamofire.request(mutableURLRequest).responseString(encoding: NSUTF8StringEncoding) { (_, _, result: String?, _) -> Void in
println("服务器返回的内容:\(result!)")
}
}

func toSoapMessage(methodName: String, params: String, paramValues: String) -> String {
var message: String = String()
message += "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
message += "<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/\">"
message += "<soap:Body>"
message += "<\(methodName) xmlns=\"http://imccoy.com/webservices/\">"
message += "<\(params)>\(paramValues)</\(params)>"
message += "</\(methodName)>"
message += "</soap:Body>"
message += "</soap:Envelope>"
println("请求消息体: \(message)")
return message
}


阅读更多
上一篇Objective-C 语法之常用数据结构
下一篇Swift中的required修饰符
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭