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
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值