Reputation: 119
I write below code to post request in soap webservices:
NSString *newstr = [[NSString alloc] initWithFormat:@"%@/Enki_UpperWs.asmx",[WebServices RequestURL]];
NSString *soapMsg = [[NSString alloc] initWithFormat:@"<?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"
"<Generate_Facility_List xmlns=\"http://tempuri.org/\">\n"
"<UserID>%d</UserID>\n"
"<LicenseKey>%d</LicenseKey>\n"
"<token>%@</token>\n"
"</Generate_Facility_List>\n"
"</soap:Body>\n"
"</soap:Envelope>",UserId,OfficeKey,strUserToken];
soapMsg=[self getcodeforkeys:soapMsg];
NSString *msgLength = [NSString stringWithFormat:@"%i", [soapMsg length]];
NSURL *myURL = [[NSURL alloc] initWithString:[newstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLCacheStorageAllowed timeoutInterval:500];
[myRequest setValue:@"text/xml; charset=UTF-8" forHTTPHeaderField:@"Content-type"];
[myRequest addValue:@"http://tempuri.org/Generate_Facility_List" forHTTPHeaderField:@"SOAPAction"];
[myRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *con = [[NSURLConnection alloc]initWithRequest:myRequest delegate:self];
self.connection = con;
[con release];
[myURL release];
[newstr release];
[soapMsg release];
return [self doParsing];
But If any parameter having special character like < Or > Or & or ' or " and if I replace it with using below function:
-(NSString*)getcodeforkeys:(NSString*)str_code{
str_code = [str_code stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
str_code = [str_code stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
str_code = [str_code stringByReplacingOccurrencesOfString:@">" withString:@">"];
str_code = [str_code stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
str_code = [str_code stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
return str_code;
}
It replace the XML characters as well..due to which i am not able to post request in XML format. I just need to replace the objects that passed and keep remain XML format as it is but How???
For example if I pass strUserToken as @"testing<&>" then it will replace its special characters but not XML string.
Can Anyone help??
Upvotes: 1
Views: 1798
Reputation: 8944
XML validated against a DTD is "Valid" XML.
You might want to try one of this approaches to make sure the final XML is valid:
UserId
,OfficeKey
,strUserToken
in your case).1.
NSString *soapMsg = [[NSString alloc] initWithFormat:@"<?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"
"<Generate_Facility_List xmlns=\"http://tempuri.org/\">\n"
"<UserID><![CDATA[%d]]></UserID>\n"
"<LicenseKey><![CDATA[%d]]></LicenseKey>\n"
"<token><![CDATA[%@]]></token>\n"
"</Generate_Facility_List>\n"
"</soap:Body>\n"
"</soap:Envelope>",UserId,OfficeKey,strUserToken]; // the text inside CDATA is not parsed as a part of XML, it might have special characters unescaped. The parser should support CDATA though
// do not escape complete document - remove the line soapMsg=[self getcodeforkeys:soapMsg];
2.
NSString *soapMsg = [[NSString alloc] initWithFormat:@"<?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"
"<Generate_Facility_List xmlns=\"http://tempuri.org/\">\n"
"<UserID>%d</UserID>\n"
"<LicenseKey>%d</LicenseKey>\n"
"<token>%@</token>\n"
"</Generate_Facility_List>\n"
"</soap:Body>\n"
"</soap:Envelope>",
[self getcodeforkeys:UserId],
[self getcodeforkeys:OfficeKey],
[self getcodeforkeys:strUserToken]]; // escape text separately
// do not escape complete document - remove the line soapMsg=[self getcodeforkeys:soapMsg];
Upvotes: 1