2013-01-09 37 views
-3

我想在iphone中調用SOAP請求。但我不知道該怎麼做。請給我一些在iphone中調用SOAP請求的代碼,如下面的代碼?在iOS中調用SOAP請求

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 

    <soap:Header> 
     <wsse:Security 
      xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
      xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
      xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="1"> 
       <wsse:UsernameToken 
        xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
        xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
          <wsse:Username>cbrown</wsse:Username> 
          <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">welcome</wsse:Password></wsse:UsernameToken> 
     </wsse:Security> 
    </soap:Header> 
    <soap:Body xmlns:ns1="http://xmlns.oracle.com/bpel/aubi/mobile/Worklist"> 
     <ns1:WorklistRetrievalREQ> 
      <ns1:WorklistType>HR_OFFER</ns1:WorklistType> 
      <ns1:Status>TODO</ns1:Status> 
      <ns1:Mode/> 
     </ns1:WorklistRetrievalREQ> 
    </soap:Body> 
</soap:Envelope> 

回答

1

這是你可以調用SOAP Web服務:

在您的.h文件中聲明:

NSMutableData  *webPortFolio; 
    NSMutableString  *soapResultsPortFolio; 
    NSURLConnection  *conn; 

    //---xml parsing--- 

    NSXMLParser   *xmlParserPortFolio; 
    BOOL    elementFoundPortFolio; 
    NSMutableURLRequest *req; 

    NSString   *theXMLPortFolio; 
    NSString   *strSoapMsg; 
    UIAlertView   *alertView; 

在您.m文件使用下面的代碼:

-(void)loadPortfolioData 
{ 

    strSoapMsg = [[NSString alloc] initWithFormat: 
        @"<?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>" 
        "<GetPortfolioList xmlns=\"http://tempuri.org/\">" 
        "<EmailID>%@</EmailID>" 
        "<Password>%@</Password>" 
        "<TradingGameID>%d</TradingGameID>" 
        "</GetPortfolioList>" 
        "</soap12:Body>" 
        "</soap12:Envelope>",gameUserName,gamePassword,gameid]; 


    //---print it to the Debugger Console for verification--- 
    NSLog(@"soapMsg..........%@",strSoapMsg); 

    NSURL *url = [NSURL URLWithString:@"http://www.abc.sirus/Process/process.asmx"]; 
    req = [NSMutableURLRequest requestWithURL:url]; 

    //---set the headers--- 

    NSString *msgLength = [NSString stringWithFormat:@"%d",[strSoapMsg length]]; 
    [req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [req addValue:@"http://tempuri.org/GetPortfolioList" forHTTPHeaderField:@"SOAPAction"]; 
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 

    //---set the HTTP method and body--- 

    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody: [strSoapMsg dataUsingEncoding:NSUTF8StringEncoding]]; 

    // [activityIndicator startAnimating]; 

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    if (conn) 
    { 
     webPortFolio = [[NSMutableData data] retain]; 
    } 
} 

而要處理響應,您可以使用以下功能:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webPortFolio setLength:0];  
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webPortFolio appendData:data]; 
} 

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{ 

    NSLog(@"error...................%@",[error description]); 
    [webPortFolio release]; 
    [connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 

    //Check the request and returns the response. 

    NSLog(@"DONE. Received Bytes: %d", [webPortFolio length]); 

    theXMLPortFolio = [[NSString alloc] 
         initWithBytes: [webPortFolio mutableBytes] 
         length:[webPortFolio length] 
         encoding:NSUTF8StringEncoding]; 

    //---shows the XML--- 

    NSLog(@"shows the XML %@",theXMLPortFolio); 
    [theXMLPortFolio release];  

    if(xmlParserPortFolio) 
    { 
     [xmlParserPortFolio release]; 
    } 
    xmlParserPortFolio = [[NSXMLParser alloc] initWithData: webPortFolio]; 
    [xmlParserPortFolio setDelegate: self]; 
    [xmlParserPortFolio setShouldResolveExternalEntities:YES]; 
    [xmlParserPortFolio parse]; 
    [webPortFolio release]; 
    [connection release]; 
} 

//---when the start of an element is found--- 
-(void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
    namespaceURI:(NSString *) namespaceURI 
    qualifiedName:(NSString *) qName 
    attributes:(NSDictionary *) attributeDict 
{ 

    if([elementName isEqualToString:@"GetPortfolioListResult"]) 
    { 
     if (!soapResultsPortFolio) 
     { 
      soapResultsPortFolio = [[NSMutableString alloc] init]; 
     } 
     elementFoundPortFolio = TRUE; 
     NSLog(@"Registration...%@",soapResultsPortFolio); 
    } 
    else if([elementName isEqualToString:@"your_tag_name"]) 
    { 
     elementFoundPortFolio = TRUE; 
    } 
    else if([elementName isEqualToString:@"your_tag_name"]) 
    { 
     elementFoundPortFolio = TRUE; 
    } 
    else if([elementName isEqualToString:@"your_tag_name"]) 
    { 
     elementFoundPortFolio = TRUE; 
    } 

} 

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string 
{ 
    if (elementFoundPortFolio) 
    { 
     [soapResultsPortFolio appendString: string]; 
    }  
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
{ 
    NSLog(@"Parser error %@ ",[parseError description]); 
} 


//---when the end of element is found--- 
-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"GetPortfolioListResult"]) 
    {   
     NSLog(@"display the soap results%@",soapResultsPortFolio); 
    } 
    else if([elementName isEqualToString:@"your_tag_name"]) 
    {   
     //Perform required action 
    } 
    else if([elementName isEqualToString:@"your_tag_name"]) 
    { 
     //Perform required action 
    } 
    else if([elementName isEqualToString:@"your_tag_name"]) 
    { 
     //Perform required action 
    } 

    [soapResultsPortFolio setString:@""]; 
    elementFoundPortFolio = FALSE; 
} 
+0

什麼gameUserName和gamePassword這裏。 –

+0

@DeepikaLalra這些是即時通信作爲我的XML Web服務參數的字符串。 – Rushi