用于拼接地址的常量
直接上代码,这块代码是封装在NetAddressFile.swift类中
import Foundation
//测试地址
let kURLHeader = "http://192.168.2.17:9090/"
//请求的命名空间
let kNameSpace = "http://tempuri.org/"
//服务地址
enum Service:String {
case UserService = "UserService.asmx"
case ProductService = "ProductService.asmx"
case StockService = "StockService.asmx"
}
//服务器接口地址
enum ServiceAction{
enum UserServiceAction:String{
case RegistAction = "Regist"
case LogonAction = "Logon"
case LogonTAction = "LogonT"
case GetParameter = "GetParameter"
}
enum ProductServiceAction:String{
case GetProductAction = "GetProduct"
case AddProductAction = "AddProduct"
}
enum StockServiceAction :String{
case AddStockAction = "AddStock"
case UpdateCountAction = "UpdateCount"
case GetStockAction = "GetStock"
}
}
以下代码是NetWorkManager类,我们所用来请求数据的类
该类里面共有四个方法:
public func postRequest(_ service:String,action:String,paramValues:String,success:@escaping SuccessBlock){}
private func getURLRequest(_ action:String,URL:Foundation.URL,soapMsg:String)->URLRequest{}
private func getURL(_ service:String,action:String)->URL{}
private func toSoapMessage(_ action: String, pams: String) -> String {}
以上四个方法中我们使用第一个方法public func postRequest(_ service:String,action:String,paramValues:String,success:@escaping SuccessBlock){}
请求数据,其他三个是为第一个服务的,以下是详细代码。
import UIKit
import Alamofire
//请求成功的回调
typealias SuccessBlock = (NSData)->Void
//请求失败的回调
typealias FaileBlock = (NSError)->Void
class NetWorkManager: NSObject{
var webData: NSMutableData!
var currentService: String = String()
var currentAction: String = String()
var successBlock: SuccessBlock?
var fauleBlock: FaileBlock?
// 请求数据的时候使用这个类就可以
public func postRequest(service:String,action:String,paramValues:String,success:SuccessBlock){
self.successBlock = success
self.currentService = service
self.currentAction = action
let URL = getURL(service, action: action)
print(URL)
let soapMsg:String = toSoapMessage(action, pams: paramValues)
let urlRequest: URLRequest = getURLRequest(action, URL: URL, soapMsg: soapMsg)
Alamofire.request(urlRequest).responseData { response in
if response.result.isSuccess{
if let successful = self.successBlock{
successful(response.data!)
}
}else{
print("请求数据失败----》\(response.result.error?.description)")
}
}
}
private func getURLRequest(_ action:String,URL:Foundation.URL,soapMsg:String)->URLRequest{
var request:URLRequest = URLRequest(url: URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 1)
request.setValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
let soapAction = kNameSpace+action
print("\(soapAction)")
request.setValue(soapAction, forHTTPHeaderField: "SOAPAction")
request.setValue("\(soapMsg.characters.count)", forHTTPHeaderField: "Content-Length")
request.httpMethod = "POST"
request.httpBody = soapMsg.data(using: String.Encoding.utf8)
return request
}
/*
Param:
service:服务地址 eg. ProductService.asmx
action: 服务端口 eg. GetProduct
Return: "http://192.168.2.17:9090/ProductService.asmx?op=GetProduct"
*/
private func getURL(service:String,action:String)->NSURL{
let urlStr = kURLHeader+service+"?op="+action
return NSURL(string: urlStr)!
}
/*
Description: 拼接请求的字符串
Param:
action:服务端口 eg. GetProduct
pams:需要上传的参数
Return: 字符串
*/
private func toSoapMessage(action: String, pams: 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 += "<\(action) xmlns=\"\(kNameSpace)\">"
message += "\(pams)"
message += "</\(action)>"
message += "</soap:Body>"
message += "</soap:Envelope>"
return message
}
}
上面的类写好之后,我们就可以使用了。
我们写了那么多就是为了,拼接出SOAP请求(参考图片中的SOAP请求)
使用方法如下:
// 直接调用这个方法就可以发起请求
比如:在我的后台方法中要求传入一个名为CountingEntryTable的数组
func postData(){
var message = "<data>"
for item in self.dataArray {
//FileDateModel是我自己定义的一个Model,可以修改为你们只的数据
let fileModel:FileDataModel = item as! FileDataModel
message += "<CountingEntryTable>"
message += "<skuCode>\(fileModel.skuCode!)</skuCode>"
message += "<skuName>\(fileModel.skuName!)</skuName>"
message += "<onHandQty>\(fileModel.onhandQty!)</onHandQty>"
message += "<countQty>\(fileModel.countQty!)</countQty>"
message += "<varianceQty>\(fileModel.varianceQty!)</varianceQty>"
message += "</CountingEntryTable>"
}
message += "</data>"
/*
现在的message消息为:
<data>
<CountingEntryTable>
<skuCode>9787512408371</skuCode>
<skuName>C语言</skuName>
<onHandQty>20</onHandQty>
<countQty>10</countQty>
<varianceQty>10</varianceQty>
</CountingEntryTable>
<CountingEntryTable>
<skuCode>9787121247453</skuCode>
<skuName>iOS开发</skuName>
<onHandQty>10</onHandQty>
<countQty>12</countQty>
<varianceQty>-2</varianceQty>
</CountingEntryTable>
</data>
*/
/*
最终的message为:
<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<AddCount xmlns="http://tempuri.org/">
<data>
<CountingEntryTable>
<skuCode>9787512408371</skuCode>
<skuName>C语言</skuName>
<onHandQty>20</onHandQty>
<countQty>10</countQty>
<varianceQty>10</varianceQty>
</CountingEntryTable>
<CountingEntryTable>
<skuCode>9787121247453</skuCode>
<skuName>iOS开发</skuName>
<onHandQty>10</onHandQty>
<countQty>12</countQty>
<varianceQty>-2</varianceQty>
</CountingEntryTable>
</data>
</AddCount>
</soap:Body></soap:Envelope>
*/
//最终的message当我们调用下面此方法后会自动生成,因为我们再`NetWorkManager`类中会进一步处理,该方法为toSoapMessage
//先创建一个networkManager对象
var networkManager: NetWorkManager = NetWorkManager()
//该方法就是我们最终使用的方法,返回data数据,数据类型就是上图中的SOAP响应
networkManager.postRequest(Service.CountingEntry.rawValue, action: ServiceAction.CountEntryServiceAction.AddCount.rawValue, paramValues: message, success:{
data in
// self.parse = NSXMLParser(data: data)
// self.parse!.delegate = self
// self.parse!.parse()
let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
print("\(string!)")
})
}