Content-Type: text/xml; charset=utf-8、SOAPActionIBOutlet 連接:UITextField *nameInput、UILabel *greeting、IBAction buttonClick:
- (void)getOffsetUTCTimeSOAP {
// 1. 構造 SOAP 信封
NSString *soapMessage = [NSString stringWithFormat:
@""
@""
@""
@""
@"%@ "
@" "
@" "
@" ", nameInput.text];
NSLog(@"Request:\n%@", soapMessage);
// 2. 構造請求
NSURL *url = [NSURL URLWithString:@"https://www.nanonull.com/TimeService/TimeService.asmx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"https://www.Nanonull.com/TimeService/GetOffsetUTCTime" forHTTPHeaderField:@"SOAPAction"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
// 3. 異步連接
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
_webData = [NSMutableData data]; // 接收緩衝
} else {
NSLog(@"❌ 連接創建失敗");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[_webData setLength:0]; // 重置緩衝
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_webData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"❌ 網絡錯誤:%@", error.localizedDescription);
[connection release];
[_webData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *xmlString = [[NSString alloc] initWithData:_webData encoding:NSUTF8StringEncoding];
NSLog(@"Response XML:\n%@", xmlString);
// 4. XML 解析
_xmlParser = [[NSXMLParser alloc] initWithData:_webData];
_xmlParser.delegate = self;
[_xmlParser setShouldResolveExternalEntities:YES];
[_xmlParser parse];
[connection release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"GetOffsetUTCTimeResult"]) {
_recordResults = YES;
_soapResults = [NSMutableString string];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (_recordResults) [_soapResults appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"GetOffsetUTCTimeResult"]) {
_recordResults = NO;
self.greeting.text = _soapResults; // 更新 UI
NSLog(@"✅ 解析結果:%@", _soapResults);
}
}
| 方案 | 優點 | 缺點 |
|---|---|---|
| AFNetworking / Alamofire + XMLMapper / SWXMLHash | 鏈式 API、自動解析、錯誤處理、單元測試友好 | 引入依賴 |
| SOAPEngine / SWSOAPEngine | WSDL 自動生成代碼、強類型模型、複雜類型支援 | 學習曲線、體積較大 |
| 手動 NSURLSession + XMLParser | 零依賴、完全可控、適合極簡項目 | 樣板代碼多、易出錯 |
建議:新項目優先考慮 REST/JSON + Codable;必須對接 SOAP 服務時,引入 SOAPEngine 由 WSDL 自動生成模型與請求代碼,避免手寫 XML。