1. 程式人生 > >關於iOS呼叫SOAP協議的WebServices介面

關於iOS呼叫SOAP協議的WebServices介面

在我們做專案的時候,往往移動客戶端是現有平臺的一個擴充套件,因此客戶端實現往受限於服務採用的技術。那麼目前大多數在一個新專案開始的時候,可能會考慮如果要考慮移動客戶端的話會提供介面,但是,有些時候服務端已經有介面,而且這個介面可能不僅僅是提供給移動平臺使用的,比如它的實現採用的是SOAP協議的WebServices實現。那麼這個時候iOS端應該要如何去做呢? 

 

//1. 建立URL

    NSURL *url = [NSURL URLWithString:@"http://www.joy11.com/WebService/LiAnService.asmx"];

    

    //2. 建立請求(post方式建立可變請求)

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    //3. 指定請求為post方式

    [request setHTTPMethod:@"POST"];

    

    //4. 準備提交的資料

    NSString *soapMessage = [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:Header>"

                             "<AuthSoapHd xmlns=\"http://tempuri.org/\">"

                             "<strUserName>lian2014</strUserName>" //填寫soap herder賬戶

                             "<strPassword>lian0403</strPassword>" //填寫soap herder密碼

                             "</AuthSoapHd>"

                             "</soap12:Header>"

                             "<soap12:Body>"

                             "<LoginLink xmlns=\"http://tempuri.org/\">"

                             "<key>%@</key>" //填寫使用者Id

                             "</LoginLink>"

                             "</soap12:Body>"

                             "</soap12:Envelope>", [AccountTool sharedAccountTool].currentAccount.uid];

    

    

    

    NSURL *url1 = [NSURL URLWithString:@"http://www.joy11.com/webservice/lianservice.asmx"];

    

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url1];

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

 

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[theRequest addValue: @"http://tempuri.org/LoginLink" forHTTPHeaderField:@"SOAPAction"];

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

[theRequest setHTTPMethod:@"POST"];

[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

程式碼為直接lain'jie