views:

48

answers:

1

I have an Array of 5 NSString. I want to send my array to my web use NSURLRequest as URL query data? Anybody can help me :)

+1  A: 

Manual Solution

There is nothing built into NSURL or NSURLRequest to embed query parameters into a URL. Your best bet would be to do it manually, like this:

NSString *encodedString1 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedString2 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedString3 = [string3 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedString4 = [string4 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedString5 = [string5 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *url = [NSString stringWithFormat: "%@?str1=%@&str2=%@&str3=%@&str4=%@&str5=%@", baseUrl, encodedString1, encodedString2, encodedString3, encodedString4, encodedString5];

Source: http://www.cocoabuilder.com/archive/cocoa/204008-nsurl-with-query-string.html

Variable Solution

Or, you can add all your strings to an NSDictionary, and use this solution.

Chetan
You need to URL-encode any strings you put into a request like this. See: http://www.rfc-editor.org/rfc/rfc1738.txt and http://mesh.typepad.com/blog/2007/10/url-encoding-wi.html
Alex Reynolds
@Alex: You're right, thanks. I've edited my answer to add the URL-encoding.
Chetan
I want to send my array to web as an array. Bc i don't know specify number of objects in array i send to web. By this way, i can read it on my web use: array[indexOfObject];
leduchuy89vn
@ledchuy89vn: I've edited my answer to add a solution for a variable number of strings.
Chetan