views:

43

answers:

1

Hi there 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]
                                             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... 

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

+3  A: 

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

Claus Broch
that, unfortunately, doesn't solve my problem. I really think the issue here is the length of my URL, because if myIdentifiers (which looks somthing like this: "234|2348|3923|2398" is short enough, the whole request works. thanks for your help anyway..
samsam
Those vertical lines in your identifier are not valid characters in an url
Claus Broch
oh well, seems like I have to say sorry :) you were right, I was wrong and I also looked at the wrong part of my code :(.. thanks again!
samsam
No problems - at least it solved yours :-)
Claus Broch