iOS開發25:使用SOAP訪問Web服務

SOAP是簡單對象訪問協議,它可當作是HTTP與XML的結合,其中XML部分是做爲HTTP報文的實體主體部分。具體信息能夠參考百度百科。web

在iOS中使用SOAP,須要咱們本身組裝XML格式的字符串,當XML字符串比較長的時候會變得很麻煩。另外,咱們在寫XML格式的字符串時也要常常使用轉義字符「\」。瀏覽器

爲了編寫咱們的SOAP應用程序,先要找一個提供SOAP服務的網站,這裏用的是http://www.webxml.com.cn,這是一個國內的提供Web服務的網站,頗有意思。咱們用到的是提供手機歸屬地查詢的服務,具體網站是http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo。用瀏覽器打開這個網站,以下圖:服務器

若在mobileCode輸入手機號碼,userID不輸入,點擊調用,則結果以下:網絡

這個結果呢不大準確,由於我輸入的號碼是動感地帶的。但不影響本文主題。app

看看剛纔那個網頁的內容,注意到SOAP 1.2標籤下的內容:ide

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?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>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

上面的這段文本就是使用SOAP 1.2的請求報文格式,就是一個HTTP請求報文,注意空行上面的那些內容中的請求行與各首部行的每一個字段名,在下面的示例中會用到。這個HTTP請求報文的實體主體部分是XML格式的一段文本,注意Body標籤之間的內容。網站

服務器的響應報文格式以下:atom

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?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>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>

咱們要用到的只有getMobileCodeInfoResult這個標籤。url

此次的例子是實現經過SOAP服務查詢手機號碼歸屬地、運行商等信息。PS:用的Xcode 4.4.1。spa

一、運行Xcode 4.4.1,新建一個Single View Application,名稱爲SOAP Test:

二、界面設計:打開ViewController.xib,設計界面以下所示:

在文本輸入框的Attribute Inspector中設置其Keyboard屬性爲Number Pad。

三、以後向ViewController.h中,爲文本輸入框建立OutLet映射,名稱爲:phoneNumber;爲「查詢」按鈕建立Action映射,事件類型爲Touch Up Inside,名稱爲:doQuery。創建映射的方法就是打開Assistant Editor,選中某一控件,按住Ctrl,拖向ViewController.h,能夠參考前面的文章。

四、在ViewController.h中添加代碼:

4.1 在@interface那行最後添加代碼

<NSXMLParserDelegate,  NSURLConnectionDelegate>

使ViewController遵照這兩個協議。前者用來解析XML,後者用於網絡鏈接。

4.2 在@end以前添加代碼

@property (strong, nonatomic) NSMutableData *webData;
@property (strong, nonatomic) NSMutableString *soapResults;
@property (strong, nonatomic) NSXMLParser *xmlParser;
@property (nonatomic) BOOL elementFound;
@property (strong, nonatomic) NSString *matchingElement;
@property (strong, nonatomic) NSURLConnection *conn;

五、在ViewController.m中添加代碼:

5.1 在@implementation以後添加代碼

@synthesize webData;
@synthesize soapResults;
@synthesize xmlParser;
@synthesize elementFound;
@synthesize matchingElement;
@synthesize conn;

5.2 實現doQuery方法

// 開始查詢
- (IBAction)doQuery:(id)sender {
    NSString *number = phoneNumber.text;
    
    // 設置咱們以後解析XML時用的關鍵字,與響應報文中Body標籤之間的getMobileCodeInfoResult標籤對應
    matchingElement = @"getMobileCodeInfoResult";
    // 建立SOAP消息,內容格式就是網站上提示的請求報文的實體主體部分
    NSString *soapMsg = [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>", number, @""];
    
    // 將這個XML字符串打印出來
    NSLog(@"%@", soapMsg);
    // 建立URL,內容是前面的請求報文報文中第二行主機地址加上第一行URL字段
    NSURL *url = [NSURL URLWithString: @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
    // 根據上面的URL建立一個請求
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    // 添加請求的詳細信息,與請求報文前半部分的各字段對應
    [req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    // 設置請求行方法爲POST,與請求報文第一行對應
    [req setHTTPMethod:@"POST"];
    // 將SOAP消息加到請求中
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
    // 建立鏈接
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) {
        webData = [NSMutableData data];
    }
}

5.3 在@end以前添加代碼

#pragma mark -
#pragma mark URL Connection Data Delegate Methods

// 剛開始接受響應時調用
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{
    [webData setLength: 0];
}

// 每接收到一部分數據就追加到webData中
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data {
    [webData appendData:data];
}

// 出現錯誤時
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error {
    conn = nil;
    webData = nil;
}

// 完成接收數據時調用
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
                                                length:[webData length]
                                              encoding:NSUTF8StringEncoding];
    
    // 打印出獲得的XML
    NSLog(@"%@", theXML);
    // 使用NSXMLParser解析出咱們想要的結果
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];
}

5.4 在@end以前添加代碼

#pragma mark -
#pragma mark XML Parser Delegate Methods

// 開始解析一個元素名
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
    if ([elementName isEqualToString:matchingElement]) {
        if (!soapResults) {
            soapResults = [[NSMutableString alloc] init];
        }
        elementFound = YES;
    }
}

// 追加找到的元素值,一個元素值可能要分幾回追加
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
    if (elementFound) {
        [soapResults appendString: string];
    }
}

// 結束解析這個元素名
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:matchingElement]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"手機號碼信息"
                                                        message:[NSString stringWithFormat:@"%@", soapResults]
                                                       delegate:self
                                              cancelButtonTitle:@"肯定"
                                              otherButtonTitles:nil];
        [alert show];
        elementFound = FALSE;
        // 強制放棄解析
        [xmlParser abortParsing];
    }
}

// 解析整個文件結束後
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    if (soapResults) {
        soapResults = nil;
    }
}

// 出錯時,例如強制結束解析
- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    if (soapResults) {
        soapResults = nil;
    }
}

六、運行

  

其中,輸入號碼時單擊查詢,打印出的響應XML以下:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
            <getMobileCodeInfoResult>151898XXXXX:江蘇 南京 江蘇移動全球通卡
            </getMobileCodeInfoResult>
        </getMobileCodeInfoResponse>
    </soap:Body>
</soap:Envelope>

上面的XML進行了縮進處理,實際上打印出來的是一行。

完整代碼:http://www.oschina.net/code/snippet_164134_13248

相關文章
相關標籤/搜索