0

I am new to iphone development.

I am trying to send NSSTRING to server from iphone application.

With following line of Code

NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                             "<soap12:Body>\n"
                             "<SaveData xmlns=\"http://tempuri.org/\">\n"
                             "<xmlstring>%@</xmlstring>\n"
                             "</SaveData>\n"
                             "</soap12:Body>\n"
                             "</soap12:Envelope>\n", StringXml
                             ];
    NSLog(@"Soap Messaage......%@",soapMessage);

    NSURL *url = [NSURL URLWithString:urlString];
    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/SaveData" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
   // [theRequest addValue: StringXml forHTTPHeaderField:@"xmlstring"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    //[theRequest setHTTPBody: [StringXml dataUsingEncoding:NSUTF8StringEncoding]];

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

but it return me Wrong reply

i.e.

System.InvalidOperationException: Request format is invalid: text/xml; charset=utf-8. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

How to over come this problem?

  • show the code for receiving data.. – AppleDelegate Sep 27 '12 at 08:40
  • -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {} -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {NSMutableData *webdata=[[NSMutableData alloc]initWithData:data]; NSString *theXML = [[NSString alloc] initWithBytes: [webdata mutableBytes] length:[webdata length] encoding:NSUTF8StringEncoding];NSLog(@" Received theXML : %@", theXML); } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {NSLog(@"ERROR with theConenction");} -(void)connectionDidFinishLoading:(NSURLConnection *)connection {} – Hi phone development Sep 27 '12 at 09:35

1 Answers1

0

Dont use the NSURLConnection just after setting the soap envelope..use this instead

NSError *WSerror;
        NSURLResponse *WSresponse;

        @try
        {

        NSMutableData *resMutableData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&WSresponse error:&WSerror];


            NSString *theResponse = [[NSString alloc]initWithBytes:[resMutableData mutableBytes] length:[resMutableData length] encoding:NSUTF8StringEncoding];
            NSLog(theResponse);

        }
        @catch( NSException* ex )
        {
            NSLog(@"Webservice Request Failed: Error %@", [WSerror code]);
        }
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27