-3

我想在 iphone 中调用 SOAP 请求。但我不知道该怎么做。请给我一些在 iphone 中调用 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>
4

2 回答 2

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;
}
于 2013-01-09T06:06:26.880 回答
1

如果项目中的soap调用数量增加,我可以推荐使用工具wsdl2objc。它将生成一个 Objective C 服务和模型定义。工具最近没有更新,也没有使用Arc,不过还是可以的。http://code.google.com/p/wsdl2objc/wiki/UsageInstructions

于 2013-01-09T07:04:01.000 回答