我这边的后台有个项目是. net 写的,还有个是 Java 写的,后者很熟悉了, alamofire 或者 afnetworking 都可以解决,前者还是第一次遇到,试了半天也总算是成功了,下面拿登录接口来详细说明,接口名称:IsExistsUsers
let soapMsg = "" +
"
"
"<IsExistsUsers xmlns=\"http://tempuri.org/\">" +
"<telphone>手机号telphone>" +
"<userPassword>密码userPassword>" +
"IsExistsUsers>" +
"" +
"\n"
上面这段是必不可少的,这是固定格式( 其他的接口只需要修改红色的接口名和蓝色的参数即可 ), IsExistsUsers接口中有两个参数,telphone,userPassword,需要注意的是:大小写和参数顺序都是固定的(有一次我就两个参数位置一前一后写错了,后台跟我说,顺序都是固定的,按照文档来写),橙色背景的是 命名空间,获取方法如下:
接下来就是一些请求设置:
let url = "http://xxxxx" 接口 URL
let request = NSMutableURLRequest(URL:NSURL(string: url)!)
let msgLength = String(soapMsg.characters.count)
request.HTTPMethod = "POST"
request.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField:"Content-Length")
request.addValue(" 这里是对应接口IsExistsUsers的 soapaction", forHTTPHeaderField: "SOAPAction")
下面是请求:
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (resp, data, error) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let e = error {
print("ERROR:----------------\(e)")
}
if let d = data {
let dataString : NSString = NSString(bytes: d.bytes, length: d.length, encoding: NSUTF8StringEncoding)!
let xml = SWXMLHash.parse(dataString as String)
let result = xml["s:Envelope"]["s:Body"]["IsExistsUsersResponse"]["IsExistsUsersResult"].element!.text!