initWithContentsOfURL似乎有“长”URL的问题

时间:2010-04-14 09:50:28

标签: iphone objective-c

在尝试从XML-Webservice加载数据时,我遇到了一个相当奇怪的问题。 webservice允许我在URL-Request中传递分隔的标识符。 因此,URL可能变得相当长(> 240个字符)。 如果我在firefox中打开所述URL,响应按计划到达,如果我执行以下代码xmlData仍为空。

NSString *baseUrl = [[NSString alloc] initWithString:[[[[kSearchDateTimeRequestTV stringByReplacingOccurrencesOfString:@"{LANG}" withString:appLanguageCode] 
                                              stringByReplacingOccurrencesOfString:@"{IDENTIFIERS}" withString:myIdentifiers]
                                             stringByReplacingOccurrencesOfString:@"{STARTTICKS}" withString:[NSString stringWithFormat:@"%@", [[startTime getTicks] descriptionWithLocale:nil]]]
                                            stringByReplacingOccurrencesOfString:@"{ENDTICKS}" withString:[NSString stringWithFormat:@"%@", [[endTime getTicks] descriptionWithLocale:nil]]]];

NSLog(baseUrl); //looks good, if openend in browser, returnvalue is ok

urlRequest = [NSURL URLWithString:baseUrl];


NSString *xmlData = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:&err]; //err is nil, therefore i guess everything must be ok... :(

NSLog(xmlData); //nothing... 

是否有任何类型的URL长度限制,你们中的任何人也会遇到同样的问题吗?什么是好的解决方法?

感谢您的帮助

SAM

1 个答案:

答案 0 :(得分:4)

在将baseURL传递给URLWithString之前,必须确保它是一个有效的URL,否则它将返回NULL。 您可以使用以下代码行将无效字符编码为percent-escape序列:

NSString *fixedURL = [baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // or any other encoding

如果您在网址中传递了无效字符,大多数浏览器会自动为您执行此操作,因此这可能是网址在Firefox中工作的原因。

克劳斯

相关问题