Error 400 when calling a .net service on the iOS app - How to solve it?

1

I need to access a webservice from my app. I've taken an example in stackoverflow to access the link service. Below is the source code for accessing the service. The problem is that I'm getting 400 error 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;
}
    
asked by anonymous 17.09.2015 / 01:51

3 answers

0

I managed to resolve. The problem was in the composition of the soap message. Replace the two double quotation marks ("") with a single quotation mark (').

- (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;
}
    
18.09.2015 / 01:13
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");
  }
    
17.09.2015 / 15:16
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"];

    
17.09.2015 / 22:57