-3

I am not getting response for following url i am loading async. http://ms:ms@www.arabcircleonline.com/index.php?do=/webservice/whisper/login_ms@gmail.com/password_ms/action_stirwhisper/whisperstirdata_{"feed_id":"89","say_something":"pqrst","privacy":0}

I am using url encoding like

NSString *urlString1 = [[NSString alloc]initWithFormat:@"http://ms:ms@www.arabcircleonline.com/index.php?do=/webservice/whisper/login_ms@gmail.com/password_ms/action_stirwhisper/whisperstirdata_{\"feed_id\":\"%@\",\"say_something\":\"%@\",\"privacy\":%@}",feedid,[alertView textFieldAtIndex:0].text,privacy];

NSData *mydata1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[urlString1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

I think there is some problem in encoding. Help will be appreciated.

alan
  • 6,705
  • 9
  • 40
  • 70
mansoor
  • 9
  • 3
  • 1
    please explain your problem properly. – Toseef Khilji Oct 17 '13 at 12:15
  • use this : http://stackoverflow.com/questions/8088473/url-encode-an-nsstrin` – Uniruddh Oct 17 '13 at 12:29
  • @Virussmca I think the problem is clear: obviously, the URL query component is not properly percent encoded. – CouchDeveloper Oct 17 '13 at 13:18
  • am sorry for the long delay in this question but to continue with my query, when i am using the way suggested by CouchDeveloper to encode the things sapereately and combining them still it is giving me the same query with do%3d.... anything to be still updated?... – mansoor Nov 04 '13 at 18:47

1 Answers1

0

Your query component needs to be "percent escaped". Furthermore, your query component may be special, in that it's structure is hierarchical. That is, a server will need to implement custom code to handle this.

The standard way for a server to decode a query component of a URL is to use the same algorithm which is defined for the decoding of a body of a "application/x-www-form-urlencoded" request. The corresponding encoding algorithm employed by the client is defined here:

application/x-www-form-urlencoded encoding algorithm

(Note: The query component is everything after the question mark '?' up until a hash character '#'!)

However, this encoding algorithm may produce percent escapes for characters that are in the "Unreserved" character set of an URL, according RFC 3986, namely the tilde ~ character. Characters in the "Unreserved" character set don't need to be escaped. More strictly, due to implications in URI comparison implementations, RFC 3986 makes this recommendation:

"For consistency, percent-encoded octets in the ranges of ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers".

Thus, a slightly modified implementation which excludes the tilde '~' from escaping may yield the best results.

The following code encodes the name or the value of a parameter. Note that parameters must be separated by a '&', and the name and value must be separated by a '='.

static NSString* form_urlencode_rfc3986(NSString* s) {
    CFStringRef charactersToLeaveUnescaped = CFSTR(" ");
    //CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@~");
    // Modified for urls (excluding '~'):
    CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@");

    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                             kCFAllocatorDefault,
                             (__bridge CFStringRef)s,
                             charactersToLeaveUnescaped,
                             legalURLCharactersToBeEscaped,
                             kCFStringEncodingUTF8));
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

Example:

Given your example URL, the query component consists of one parameter:

parameter name: "do"

parameter value: "/webservice/whisper/login_ms@gmail.com/password_ms/action_stirwhisper/whisperstirdata_{"feed_id":"89","say_something":"pqrst","privacy":0}"

The encoded query component will then look as below:

do=%2Fwebservice%2Fwhisper%2Flogin_ms%40gmail.com%2Fpassword_ms%2Faction_stirwhisper%2Fwhisperstirdata_%7B%22feed_id%22%3A%2289%22%2C%22say_something%22%3A%22pqrst%22%2C%22privacy%22%3A0%7D

Community
  • 1
  • 1
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • i have tried to use this but still no results. The query i am executing contains characters like "{" , "}" and "_" along with double quote. I have used NSUTF8StringEncoding but still no results. – mansoor Oct 17 '13 at 12:44
  • First, it should be clear that your URL given in the first post is not valid, because the query is not percent encoded. Then, your original query string is no problem for the percent encoding. When the server *decodes* it, it should yield your original string. – CouchDeveloper Oct 17 '13 at 13:01
  • NSString *urlString1 = [[NSString alloc]initWithFormat:@"http://admin:testsite@www.arabcircleonline.com/index.php?do=/webservice/whisper/login_chauhankevalp@gmail.com/password_keval/action_stirwhisper/whisperstirdata_{\"feed_id\":\"%@\",\"say_something\":\"%@\",\"privacy\":%@}",feedid,[alertView textFieldAtIndex:0].text,privacy]; NSString *encodedString=[self encodeURL:urlString1]; NSData *mydata1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:encodedString]]; – mansoor Oct 17 '13 at 14:06
  • this is what i am using to encode the url and i am getting the output as http%3A%2F%2Fadmin%3Atestsite%40www.arabcircleonline.com%2Findex.php%3Fdo%3D%2Fwebservice%2Fwhisper%2Flogin%5Fchauhankevalp%40gmail.com%2Fpassword%5Fkeval%2Faction%5Fstirwhisper%2Fwhisperstirdata%5F%7B%22feed%5Fid%22%3A%2288%22%2C%22say%5Fsomething%22%3A%22go%22%2C%22privacy%22%3A0%7D which i am sending to server as url but still it is giving response as blank I don't know where i am mistaking the thing – mansoor Oct 17 '13 at 14:08
  • Only encode the *query component*! The query starts after the '?', and it contains *one* parameter. The param's *name* equals "on", and the param's *value* equals "/webservice/whisper/ ...". Encode *name* and *value* separately using above function, then compose a parameter: `NSString* param = [NSString stringWithFormat:@"%@=%@", encodedName, encodedValue];`. You have only one param, but if you had more, params will be separated by a '&', e.g. @"&" in order to become a full query component. So, in your case `queryString = param`. Append queryString to the URL, after '?' – CouchDeveloper Oct 17 '13 at 14:19
  • http://admin:testsite@www.arabcircleonline.com/index.php?do%3D%2Fwebservice%2Fwhisper%2Flogin%5Fchauhankevalp%40gmail.com%2Fpassword%5Fkeval%2Faction%5Fstirwhisper%2Fwhisperstirdata%5F%7B%22feed%5Fid%22%3A%2288%22%2C%22say%5Fsomething%22%3A%22bold%22%2C%22privacy%22%3A0%7D this is what it is outputting with this way, but still not working – mansoor Oct 17 '13 at 14:37
  • Your result is not correct, please read more careful! (e.g. it should be `do=%2Fwebservice ...` and yours is actually `do%3D%2Fwebservice ...` – CouchDeveloper Oct 17 '13 at 15:30
  • I am sorry for the long delay in this question but to continue with my query, when i am using the above way to encode the things sapereately and combining them still it is giving me the same query with do%3d.... Please help... – mansoor Nov 04 '13 at 18:41
  • You need to encode the key "do" -> yields "do" and the value "/webservice.." which yields "%2Fwebservice..". THEN, create a parameter-string by concatenating encoded-key "do" and a equal sign "=" and encoded-value "%2Fwebservice..", which yields: "do=%2Fwebservice..". – CouchDeveloper Nov 04 '13 at 18:49
  • but before weservice there is / in the query so the output is like %3D%2Fwebservice and the final string is coming as do%3D%2Fwebservice after ? – mansoor Nov 04 '13 at 18:54
  • i have got the string as http://admin:testsite@www.arabcircleonline.com/index.php?do=%2Fwebservice%2Fwhisper%2Flogin_chauhankevalp%40gmail.com%2Fpassword_keval%2Faction_whisper%2Fwhisperdata_%7B%22user_status%22%3A%22test%22%2C%22privacy%22%3A0%2C%22privacy_comment%22%3A0%7D but still it is giving nil response.. – mansoor Nov 04 '13 at 19:10
  • OK, that means, you now got the encoding correctly, congrats! :) (You should accept the answer.) But now, you also get the next issue. Likely, an authorization/authentication problem. The browser can handle that apparently (using well know certificates installed in the system). On the device, this may require to tailor the authentication handling. `initWithContentsOfURL` is likely not appropriate to solve such kinds of issue. You should now switch to NSULRConnection and delegates. Please create a new question if you have other problems. Please accept and vote if this was useful for you. – CouchDeveloper Nov 04 '13 at 19:15
  • I have created a new post for this, please have a look http://stackoverflow.com/questions/19775533/response-not-coming-with-initwithcontentsofurl It is not working with NSURLConnection as well – mansoor Nov 04 '13 at 19:42