2015-04-07 83 views
0

我在肥皂請求中發送數據數組時遇到問題。如何通過ios中的soap請求發送數據數組?

使用下面的代碼,我可以得到低於響應。

SOAP消息requets:

NSString *soapMessage=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
          "<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/\" xmlns=\"http://xmlns.oracle.com/Enterprise/Tools/services/TEST_SRVC\">\n" 
          "<soap:Body>\n" 
          "<TEST_SRVC_OP/>\n" 
          "<OPRID>%@</OPRID>\n" 
          "<PWD>%@</PWD>\n" 
          "<REQUEST_ID>%@</REQUEST_ID>\n" 
          "<PORT_CD>%@</PORT_CD>\n" 
          "<SIZE_CD>%@</SIZE_CD>\n" 
          "</soap:Body>\n" 
          "</soap:Envelope>\n",strUserName,strPassword,strRequestID,strSize] ; 

生成的SOAP消息:

<?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/" xmlns="http://xmlns.oracle.com/Enterprise/Tools/services/TEST_SRVC"> 
<soap:Body> 
<TEST_SRVC_OP/> 
<OPRID> username </OPRID> 
<PWD> passowrd </PWD> 
<REQUEST_ID> R123 </REQUEST_ID> 
<PORT_CD> TEST1 </PORT_CD> 
    <SIZE_CD>40</SIZE_CD> 
</soap:Body> 
</soap:Envelope> 

但是,我需要生成這樣的SOAP消息。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://xmlns.oracle.com/Enterprise/Tools/services/TEST_SRVC" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Body> 
     <TEST_SRVC_OP /> 
     <OPRID>username</OPRID> 
     <PWD>passowrd</PWD> 
     <REQUEST_ID>R123</REQUEST_ID> 
     <Detailrow> 
     <PORT_CD>TEST1</PORT_CD> 
     <SIZE_CD>40</SIZE_CD> 
     </Detailrow> 
     <Detailrow> 
     <PORT_CD>TEST2</PORT_CD> 
     <SIZE_CD>60</SIZE_CD> 
     </Detailrow> 
    </soap:Body> 
</soap:Envelope> 

回答

0
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]; 
    } 
0

如果你有dataPORT_CDSIZE_CD或不那麼像create例如如下:

NSString *strUserName [email protected]"username"; 
NSString *strPassword [email protected]"password"; 
NSString *strRequestID [email protected]"R123"; 

//Here all port_cd with size_cd here 
NSArray *arrData = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"TEST1",@"portcd",@"50",@"sizecd",nil],[NSDictionary dictionaryWithObjectsAndKeys:@"TEST2",@"portcd",@"60",@"sizecd",nil],[NSDictionary dictionaryWithObjectsAndKeys:@"TEST3",@"portcd",@"70",@"sizecd",nil],nil]; 

//create soap message 
NSString *soapMessage=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<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/\" xmlns=\"http://xmlns.oracle.com/Enterprise/Tools/services/TEST_SRVC\">\n" 
         "<soap:Body>\n" 
         "<TEST_SRVC_OP/>\n" 
         "<OPRID>%@</OPRID>\n" 
         "<PWD>%@</PWD>\n" 
         "<REQUEST_ID>%@</REQUEST_ID>\n",strUserName,strPassword,strRequestID]; 

//Append port_cd and size_cd here to soap message 
for (NSDictionary *dictData in arrData) 
{ 
    NSString *strPortCD = [dictData objectForKey:@"portcd"]; 
    NSString *strSizeCD = [dictData objectForKey:@"sizecd"]; 
    soapMessage = [soapMessage stringByAppendingString:[NSString stringWithFormat:@"<Detailrow>\n<PORT_CD>%@</PORT_CD>\n<SIZE_CD>%@</SIZE_CD>\n</Detailrow>\n",strPortCD,strSizeCD]]; 
} 

soapMessage = [soapMessage stringByAppendingString:@"</soap:Body>\n</soap:Envelope>"]; 

NSLog(@"soapMessage : %@",soapMessage);