开发者

initWithContentsOfURL seems to have issues with "long" URLs

开发者 https://www.devze.com 2022-12-27 08:35 出处:网络
I\'m facing a rather strange Issue when trying to load data from an XML-Webservice. The webservice allows me to pass separated identifiers within the URL-Request.

I'm facing a rather strange Issue when trying to load data from an XML-Webservice. The webservice allows me to pass separated identifiers within the URL-Request. It is therefore possible for the URL to become rather long (>240 characters). If I open said URL in firefox the response arrives as planned, if I execute the following code xmlData remains empty.

NSString *baseUrl = [[NSString alloc] initWithString:[[[[kSearchDateTimeRequestTV stringByReplacingOccurrencesOfString:@"{LANG}" withString:appLanguageCode] 
                                              stringByReplacingOccurrencesOfString:@"{IDENTIFIERS}" withString:myIdentifiers]
                                             stringByReplacingOccurrencesOfStri开发者_如何学Gong:@"{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... 

is there any sort of URL-Length restriction, does the same problem happened to anyone of you as well? whats a good workaround?

thanks for your help

sam


Before passing baseURL to URLWithString you must ensure that it is a valid url otherwise it will return NULL. You can use the following line of code to encode non-valid characters to percent-escape sequence:

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

Most browsers will actually do this automatically for you if you pass invalid characters in the URL, so this might be the reason the url works in Firefox.

Claus

0

精彩评论

暂无评论...
验证码 换一张
取 消