Search code examples
objective-cweb-servicessoapwsdl

transforming SOAP response into NSString


NSString *GetExRequest=[NSString stringWithFormat:
                        @"<?xmlversion=\"1.0\"encoding=\"utf-8\"?>\n"
                        "<soap:Envelopexmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                        "<soap:Body>\n"
                        "<GetEXRatexmlns=\"Web Services\">\n"
                        "<AGENT_CODE>xyzzyx</AGENT_CODE>\n"
                        "<USER_ID>12345689</USER_ID>\n"
                        "<PASSWORD>dkasdja</PASSWORD>\n"
                        "<AGENT_SESSION_ID>xyz1234</AGENT_SESSION_ID>\n"
                        "<TRANSFERAMOUNT>1000</TRANSFERAMOUNT>\n"
                        "<PAYMENTMODE>c</PAYMENTMODE>\n"
                        "<CALC_BY>p</CALC_BY>\n"
                        "<PAYOUT_AGENT_ID>20100008</PAYOUT_AGENT_ID>\n"
                        "<PAYOUT_COUNTRY>Bangladesh</PAYOUT_COUNTRY>\n"
                        "</GetEXRate>\n"
                        "</soap:Body>\n"
                        "</soap:Envelope>\n"];
NSLog(@"%@",GetExRequest);
NSURL *url = [NSURL URLWithString:@"https://www.prabhuusa.com/SendWsv2/txnservice.asmx?wsdl"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [GetExRequest length]];
[theRequest addValue:@"www.prabhuusa.com" forHTTPHeaderField:@"Host"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"WebServices/GetEXRate" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [GetExRequest dataUsingEncoding:NSUTF8StringEncoding]];

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

    if( theConnection )
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"theConnection is NULL");
}

I am trying to consume this WSDL definition by sending this SOAP request. I wanted to store the response onto a string and the parse it.

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [_webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [_webData mutableBytes] length:[_webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"WEBDATA::: %@",theXML);
}

But the Response string is blank. I checked this webservice with the SOAP request I sent using SOAP Client and received a XML response.. but i could not get a response in objective C. Please find me a solution

I have this in my didRecieveResponse and didRecieveData methods

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    [_webData setLength: 0];
}

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

Solution

  • There are two possible issues that leap out at me:

    1. You never show the instantiation of the webData property. Check to make sure it is not nil.

    2. Your request appears to be missing some spaces. Shouldn't it be as follows?

      NSString *GetExRequest= @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                              "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                              "<soap:Body>\n"
                              "<GetEXRate xmlns=\"Web Services\">\n"
                              "<AGENT_CODE>xyzzyx</AGENT_CODE>\n"
                              "<USER_ID>12345689</USER_ID>\n"
                              "<PASSWORD>dkasdja</PASSWORD>\n"
                              "<AGENT_SESSION_ID>xyz1234</AGENT_SESSION_ID>\n"
                              "<TRANSFERAMOUNT>1000</TRANSFERAMOUNT>\n"
                              "<PAYMENTMODE>c</PAYMENTMODE>\n"
                              "<CALC_BY>p</CALC_BY>\n"
                              "<PAYOUT_AGENT_ID>20100008</PAYOUT_AGENT_ID>\n"
                              "<PAYOUT_COUNTRY>Bangladesh</PAYOUT_COUNTRY>\n"
                              "</GetEXRate>\n"
                              "</soap:Body>\n"
                              "</soap:Envelope>\n";