使用XML字符串中的特殊字符发布请求

时间:2013-02-27 09:19:01

标签: iphone ios ipad

我在下面编写代码以在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];

但是如果任何具有特殊字符的参数如&lt;或者&gt;或者&amp;或'或'如果我用以下功能替换它:

-(NSString*)getcodeforkeys:(NSString*)str_code{
    str_code = [str_code stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    str_code = [str_code stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
    str_code = [str_code stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
    str_code = [str_code stringByReplacingOccurrencesOfString:@"'" withString:@"&apos;"];
    str_code = [str_code stringByReplacingOccurrencesOfString:@"\"" withString:@"&quot;"];
    return str_code;
} 

它也替换了XML字符..因为我无法以XML格式发布请求。我只需要替换传递的对象并保持原样的XML格式但是如何???

例如,如果我将strUserToken传递为@“testing&lt;&amp;&gt;”然后它将替换其特殊字符,但不替换XML字符串。

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

XML Validation

  

针对DTD验证的XML是“有效”XML。

您可能希望尝试使用其中一种方法来确保最终的XML有效:

  1. 使用CDATA部分作为文字内容。
  2. 单独转义文字内容(在您的案例中为UserIdOfficeKeystrUserToken

  3. 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];