iphone之XML字符串中带有特殊字符的发布请求
Demo
阅读:46
2025-06-02 22:19:02
评论:0
我在下面的代码中编写了肥皂Web服务中的请求:
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];
但是,如果任何具有特殊字符的参数(例如 <或> 或&或'或“)并且如果我使用以下功能将其替换:
-(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;
}
它也替换了XML字符。由于该原因,我无法以XML格式发布请求。我只需要替换传递的对象,并保持XML格式不变,但如何做?
例如,如果我将strUserToken传递为@“testing <&>”,则它将替换其特殊字符,而不替换XML字符串。
有人可以帮忙吗? 或>
请您参考如下方法:
XML Validation:
根据DTD验证的XML是“有效” XML。
您可能想尝试以下一种方法,以确保最终的XML有效:
UserId,OfficeKey和strUserToken)。 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];
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



