关闭

Alamofire怎么请求WebService

686人阅读 评论(0) 收藏 举报

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
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2418次
    • 积分:40
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条