Error 400 when calling a. net service in the iOS app - How to resolve?

Asked

Viewed 1,300 times

1

I need to access a webservice of my app. I took an example in stackoverflow to access the service http://www.cgsapi.com/CGSWebService.asmx. Below is the source code to access the service. The problem is that I am getting error 400 in the Response (header) and no content in the output. Does anyone have any idea what’s wrong?

-(BOOL)callWebService {
    NSString *soapMessage = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:cgs=""http://www.cgsapi.com/""><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>";

    NSURL *url = [NSURL URLWithString:@"http://www.cgsapi.com/CGSWebService.asmx"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSError *error;

    request.HTTPMethod = @"POST";
    request.HTTPBody = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];

    [request addValue:@"www.cgsapi.com" forHTTPHeaderField:@"Host"];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:[NSString stringWithFormat:@"%i", soapMessage.length] forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"http://www.cgsapi.com/GetSystemStatus" forHTTPHeaderField:@"SOAPAction"];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"response: %@", response);
        NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"output: %@", output);
        if (error !=nil) {
            NSLog(@"error: %i %@", error.code, error.description);
        }
    }];

    [task resume];

    return true;
}

3 answers

0

Apparently the problem is in the Content-Type you are using, I think the server is configured differently.

Try this with your same code: [request addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

  • Actually the 400 error is no longer occurring. But I had to replace in the SOAP message as "" by '. Now the status code is 415 and the message "The server cannot service the request because the media type is Unsupported." Nsstring *soapMessage = @"<Soap:Envelope xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:Soap='http://schemas.xmlsoap.org/soap/envelope/'><Soap:Body><Getsystemstatus xmlns='http://www.cgsapi.com/'/></Soap:Body></Soap:Envelope>";

  • I got it! The problem is really in the two double quotes I was using to compose the Soap message. I’m back to Content-Type for text/xml The result is status code: 200 Below I’ll post the resolution.

  • Glad you made it :) Good luck with the rest.

  • Thanks anyway Now I’ll work on the return parse

0


I managed to fix it. The problem was in the composition of the Soap message. Replace the two double quotes ("") with a single quote (').

- (BOOL)validateCode:(NSString *)code {
    NSString *soapMessage = @"<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><GetSystemStatus xmlns='http://www.cgsapi.com/'/></soap:Body></soap:Envelope>";

    NSURL *url = [NSURL URLWithString:@"http://www.cgsapi.com/CGSWebService.asmx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSError *error;

    request.HTTPMethod = @"POST";
    request.HTTPBody = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];

    [request addValue:@"www.cgsapi.com" forHTTPHeaderField:@"Host"];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:[NSString stringWithFormat:@"%i", soapMessage.length] forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"http://www.cgsapi.com/GetSystemStatus" forHTTPHeaderField:@"SOAPAction"];

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

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"response: %@", response);
        NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"output: %@", output);
        if (error !=nil) {
            NSLog(@"error: %i %@", error.code, error.description);
        }
    }];

    [task resume];

    return true;
}

0

no. h

 NSMutableData *myData; 

no . m

NSString *jsonPost = [NSString stringWithFormat:@""];
NSURL *url = [NSURL URLWithString:@"http://www.cgsapi.com/CGSWebService.asmx"];

NSData *postData = [jsonPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.timeoutInterval = 7.0;

[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
myData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];

    if(conn){ 
        NSLog(@"iniciando envio");
        }



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

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
    NSLog(@"terminei! estou alocado em myData");
  }
  • Hello @Williammonteiro It didn’t work. I converted the receive content and the error message is: Server was Unable to process request. ---&gt; Root element is Missing.

  • Probably that’s parser error, which you’re using.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.