"Send NSString to windows Web Service through SOAP"

Mon 14 June 2010

- (void) Send :(NSString*) tarXML

{

    recordResults = FALSE;

    

    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/\">\n"

                             "<soap:Body>\n"

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

                             "<tarData>%@</tarData>\n"

                             "</TestDecrypt>\n"

                             "</soap:Body>\n"

                             "</soap:Envelope>\n", tarXML

                             ];

    NSLog(soapMessage);

    

    NSURL *url = [NSURL URLWithString:@"http://sergeynotebook/WebService1/Service1.asmx"];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

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

    

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

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

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

    [theRequest setHTTPMethod:@"POST"];

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

    

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

    

    if( theConnection )

    {

        webData = [[NSMutableData data] retain];

    }

    else

    {

        NSLog(@"theConnection is NULL");

    }

    

}

 

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

{

    [webData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [webData appendData:data];

}

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

{

    NSLog(@"ERROR with theConenction");

    [connection release];

    [webData release];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

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

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSLog(theXML);

    [theXML release];

    

    

    [connection release];

    [webData release];

}