iOS webservice SOAP 請求


1. Web Service技術, 能使得運行在不同機器上的不同應用無須借助附加的、專門的第三方軟件或硬件, 就可相互交換數據或集成。依據Web Service規范實施的應用之間, 無論它們所使用的語言、 平台或內部協議是什么, 都可以相互交換數據。Web Service是自描述、 自包含的可用網絡模塊, 可以執行具體的業務功能。Web Service也很容易部署, 因為它們基於一些常規的產業標准以及已有的一些技術,諸如標准通用標記語言下的子集XML、HTTP。Web Service減少了應用接口的花費。Web Service為整個企業甚至多個組織之間的業務流程的集成提供了一個通用機制。

簡單來說 webservice 請求就是是往服務器POST XML數據;

然后服務器響應得到的也是XML數據;

2. 如下使用iOS NSUrlConnection 接口進行webservice 請求示例;

 測試使用的接口 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

 根據接口描述使用了兩種版本的soap請求:soap1.1,soap1.2 兩種區別在於 soap請求體xml的格式上;

 

實現原理其實根據接口上的soap體描述組合xml包;

    //SOAP 1.1
- (void)soapv11Request
{
    NSURL *url = [NSURL URLWithString:@"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    req.HTTPMethod = @"POST";
    [req setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"" forKey:@""];
    [req setValue:@"http://WebXml.com.cn/getMobileCodeInfo" forHTTPHeaderField:@"SOAPAction"];
    
    NSString *reqBody = [NSString stringWithFormat:@"<?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>\
                         <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\
                         <mobileCode>%@</mobileCode>\
                         <userID></userID>\
                         </getMobileCodeInfo>\
                         </soap:Body>\
                         </soap:Envelope>",self.phoneNumTF.text];

    NSData *reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
    req.HTTPBody = reqData;
    
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError) {
            NSLog(@"Error:%@",connectionError);
        }
        else
        {
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }
        
        
    }];
}
//SOAP 1.2
- (void)soapv12Request
{
    NSURL *url = [NSURL URLWithString:@"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    req.HTTPMethod = @"POST";
    [req setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    NSString *reqBody = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
                         <soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\
                         <soap12:Body>\
                         <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\
                         <mobileCode>%@</mobileCode>\
                         <userID></userID>\
                         </getMobileCodeInfo>\
                         </soap12:Body>\
                         </soap12:Envelope>",self.phoneNumTF.text];
    
    NSData *reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
    req.HTTPBody = reqData;
    
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError) {
            NSLog(@"Error:%@",connectionError);
        }
        else
        {
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }
        
        
    }];
}

   示例工程下載:https://github.com/cocoajin/TDDDemo/tree/master/WebServiceTest

3. 在實際應用中,我們可以使用一些xml處理的相關第三方類庫來組合soap體;

   組合好soap請求體之后,往指定接口POST 請求數據即可;

   也有專門處理SOAP請求的類庫:https://github.com/priore/SOAPEngine 可以了解一下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM