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;
}
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>";
– Celso Melero
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.
– Celso Melero
Glad you made it :) Good luck with the rest.
– Murilo Alborghette
Thanks anyway Now I’ll work on the return parse
– Celso Melero