URLstring class for iPhone - iphone

I am using the NSURL class in the iPhone framework foundation library to create a url request to a server. For some reason, I am getting back a nil for only this particular URL.
I don't understand why. Any reason?
The Objective C code is :
NSURL *scriptURL= [NSURL URLWithString:scriptURLString];
where scriptURLString is an NSString with value give below:
http://myserver/cgi-bin/AdsERsubset.cgi?user=139&fieldList=employee_num,mbox_num,last_name,first_name,disp_emp_num,company_num,TECH_PRIVILEDGE,printer_code,desktop_cos,id,supv_emp_num&action=first&searchkey=1 with proxy=DIRECT
The above URL is a CGI request.
Is there a special character I need to escape here?
Please advice. Thanks in advance.

The spaces in the searchkey should be escaped.
Use stringByAddingPercentEscapesUsingEncoding on the url string before passing it to URLWithString:
NSURL *scriptURL= [NSURL URLWithString:[scriptURLString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

...&searchkey=1 with proxy=DIRECT
Your missing an & aren't you?
I'd imagine it would be more like:
...&searchkey=1&with%20proxy=DIRECT
or
...&searchkey=1&withproxy=DIRECT

Related

Decode REST Url in Objective-C

I'm creating a module that receive and REST URL and need to match a pattern and extract the parameters
For example:
the URL "http://Product/1" should match the pattern "http://Product/{productId:long}"
and return the Dictionary with productId as a key and "1" as the value in as long
Does anyone knows about a Framework for IPhone that does it, or at least some of it?
NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.
All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.
NSString *path = [[#"path+with+spaces"
stringByReplacingOccurrencesOfString:#"+" withString:#" "]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

I wanna put an xml file content into a URL, can I?

NSURL *url = [NSURL URLWithString:#"appName://<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
But the url = nil. I found if I remove the "<" and ">" signs, it will be ok.
So, the two signs cannot be used in a URL? Should I replace the signs with another one?
Thanks!
I solved this problem.
Before I do NSURL *url = [NSURL URLWithString:#"...."];
I first do:
NSString *urlStr = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
And when parse this url on the receiver side, I firstly do:
NSString *urlString = [[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // url is a NSURL
Why would you not just POST the document in the body of the request?
GET requests are not designed for this. You may get into difficulty if there are UTF-8 chars in the document.
Also What is the maximum length of a URL in different browsers? implies a max of 2083 chars.
You're into a very browser/proxy dependent scenario.
Theoretically yes. You can use urlEncode to do this.
The HTTP protocol does not place any limit on the length of the URL.
RFC says:
Note: Servers ought to be cautious about depending on URI lengths
above 255 bytes, because some older client or proxy implementations
might not properly support these lengths.

iOS Develoment: Why is my NSURLConnection failing with a "bad URL" error for only some users?

I have an iOS app that requests JSON data from my Rails 3 app, hosted on Heroku, and it works great on my device and for many other users, except one. I have one user who has told me that my app fails to retrieve the JSON data, so I had her send me some log data and the log showed the NSURLConnection delegate method didFailWithError is being called and the error description reads "bad URL". Why is this error occurring and why is it ONLY occurring on just some devices instead of all devices?
Here's my code,
-(void)getTournamentInfoWithUsername:(NSString*)username
{
NSString *urlString = [NSString stringWithFormat:#"http://myapp-tourney.heroku.com/tournaments/next.json?username=%#", username];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[self setUrlConnection:[[NSURLConnection alloc] initWithRequest:request delegate:self]];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
[MyLog logError:[NSString stringWithFormat:#"%# - %# - %# - %#", [error localizedDescription], [error localizedFailureReason], [error localizedRecoveryOptions], [error localizedRecoverySuggestion]]];
}
and the log shows...
bad URL - (null) - (null) - (null)
Thanks so much for all your wisdom!
It doesn't look like you are encoding the username. Are there spaces or other special characters in the username? Look into using the NSString method stringByAddingPercentEscapesUsingEncoding:.
It is likely that this depends on the specific URL containing characters that are not allowed for a URL.
You can try and escape the URL:
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding`
I'm not sure how if at all it relates to your issue, but changing the Region in the Settings App had an affect on my app's ability to download. It was spitting out the same Bad URL error if the region was not set to United States. It might explain why some users are getting errors and not others.
This kind of issue may will come due to argument data having special characters(ie. Andy & Co). SOAP automatically will reject this request and returns Bad URL or Bad Request. So, before you sending the data please ensure the data having special characters like &,<,>...
If you found & in your string data , replace with <&>amp; to that string(eg.Andy <&>amp; Co). Hope this input will help for you.
Note: Before you use,Please remove start(<>) and end tag with that symbol.

Encoding on NSURLConnection

I need to load data from a REST service, but the result is always (null) when I have any accentuation. I know this is an encoding problem but NSUrlConnection gives me no solution. The best result I got is to have all my text with strange codes in the place of the accentuated characters.
I already tried to use NSUrl like the following code and it works, but I need to set two headers for this connection.
NSURL *nsurl = [[NSURL alloc] initWithString:url];
NSString *data = [[NSString alloc] initWithContentsOfURL:nsurl encoding: NSUTF8StringEncoding error: nil];
Well I could have two solutions for this taks, adding headers do NSUrl connection or manage to make the encoding to work in NSURLConnection.
Does anyone have a solution?
You can add header fields for your request in an NSMutableURLRequest instance with setValue:forHTTPHeaderField:

objective c http query

Hey, Iam new to objective c and really dont know much about it. I have been given a query that is gonna I need to send data in to the server.Query is like this http://abc.com/insertipademail.php?name=name&email=email I need to enter the name and email I have constructed the string But I dont know how to send it to the server. Can someone help me out please. Or point me in the right direction. Thanks
For starters, take a look a NSString's stringWithContentsOfURL:encoding:error: method. You could do something like:
// NSString * myURLString = whatever you do to build the url
NSURL * myURL = [NSURL URLWithString: myURLString];
NSString * response = [NSString stringWithContentsOfURL: myURL encoding: NSUTF8StringEncoding error: NULL];
NSLog(#"The response was: %#", response);
As written, this will ignore all errors and perform the request synchronously. In a real app, you probably want to handle any error that occur, and perhaps perform the request in the background. See the URL Loading System Programming Guide for further documentation. You can also try using any of several open source libraries such as those suggested in David M.'s answer.
I like the library ASIHTTPRequest. There is also HTTPRiot.