專案裡需要讓 iOS 端接上既有的 SOAP 服務,整理成兩種可行路徑:一是用工具把 WSDL 轉成 Objective-C 類別,二是自己組 SOAP 封包直接發請求。
https://xxx.xxx.asmx,在網址後面加上 ?wsdl 取得描述檔。.wsdl,例如 xxxxService.asmx.xml.wsdl。呼叫端看起來像這樣:
- (NSString *)skHkshListOfpagenow2:(NSInteger)aPagenow pagesize:(NSInteger)aPagesize {
MURPXzshServiceSoapBinding *binding = [[MURPXzshServiceSoapBinding alloc]
initWithAddress:[NSString stringWithFormat:@"%@%@", self.jjjj, XZSH_SERVICE]];
binding.logXMLInOut = YES; // 可直接觀察收送的 XML,方便除錯
MURPXzshService_HkshList *parm = [[MURPXzshService_HkshList alloc] init];
parm.xxxx = [umcid stringValue];
parm.pagenow = [NSNumber numberWithInteger:aPagenow];
parm.pagesize = [NSNumber numberWithInteger:aPagesize];
MURPXzshServiceSoapBindingResponse *resp = [binding HkshListUsingParameters:parm];
for (id mine in resp.bodyParts) {
if ([mine isKindOfClass:[MURPXzshService_HkshListResponse class]]) {
return [mine HkshListResult];
}
}
return nil;
}
這裡的服務名稱是 MURPXzshService,Binding 為 MURPXzshServiceSoapBinding,被呼叫的方法為 HkshList。這個做法最大的好處是型別明確、參數不必手動拼 XML,缺點是工具產生的程式碼量龐大,且服務端介面一變就得重新產生。
如果不想引入額外工具,也可以自己組出 SOAP Envelope,用 NSURLConnection 送出,再用系統內建的 NSXMLParser 解析回應。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *webServiceBodyStr = [NSString stringWithFormat:
@"<Jsjy_yjy xmlns=\"https://murpcn.com/murpwebservice/\">"
"<xxx>34192</xxx>"
"<pagenow>1</pagenow>"
"<pagesize>20</pagesize>"
"</Jsjy_yjy>"];
NSString *webServiceStr = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"https://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"https://www.w3.org/2001/XMLSchema\" "
"xmlns:soap=\"https://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n%@\n</soap:Body>\n</soap:Envelope>", webServiceBodyStr];
NSString *SOAPActionStr = [NSString stringWithFormat:@"https://xxx.com/murpwebservice/%@", @"Jsjy_yjy"];
NSURL *url = [NSURL URLWithString:@"https://xxxx/key/MurpjsjyService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%ld", (unsigned long)webServiceStr.length];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];
[theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSLog(@"connection = %@", theConn);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str); // 取得 XML 字串後交給解析器
NSXMLParser *par = [[NSXMLParser alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
[par setDelegate:self];
[par parse];
}
#pragma mark - NSXMLParser 委派
- (void)parserDidStartDocument:(NSXMLParser *)parser { }
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict { }
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { }
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { }
- (void)parserDidEndDocument:(NSXMLParser *)parser { }
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { }
兩個環節最容易出錯:一是 SOAPAction 標頭必須與服務描述一致,否則會收到 HTTP 500;二是 Content-Length 要用位元組數而非字元數,中英混雜時特別容易算錯。實務上建議把 XML 解析結果對應到 model 物件,並在解析器委派裡做好狀態暫存。