URL encode implementation

 
Has anyone already coded url encoder in MQL4, that could be paired to the PHP urldecode()?
 

I could not wait, here is what I used, seems to work.

   string urlencode(string value) {
      int len = StringLen(value);
      MT4String encodedValue;
      uchar characters[];
      StringToCharArray(value,characters);
      for (int i = 0; i<len ;i++) {
         encodedValue.append(StringFormat("%%%02x", characters[i]));
      }
      return encodedValue.toString();
   }
 
Ovo:

I could not wait, here is what I used, seems to work.

(That looks fine but, as you know, you are unnecessarily encoding alphanumeric characters, and therefore you could exceed the maximum permitted length of a URL more easily. Full list of unreserved characters at http://tools.ietf.org/html/rfc3986#section-2.3)
 
gchrmt4:
(That looks fine but, as you know, you are unnecessarily encoding alphanumeric characters, and therefore you could exceed the maximum permitted length of a URL more easily. Full list of unreserved characters at http://tools.ietf.org/html/rfc3986#section-2.3)


Thank you, I did not perform thorough investigation. If I remember the limit was somewhere about 4k, so it is not a real threat for the purpose I need. My motto: First make it working, then optimize :)

 
Ovo:


Thank you, I did not perform thorough investigation. If I remember the limit was somewhere about 4k, so it is not a real threat for the purpose I need. My motto: First make it working, then optimize :)

(You are still very unlikely to hit it in real life... but if you are using Wininet then the limit is more like 2k: http://stackoverflow.com/a/3208930. In practice, allowing for "static" elements such as the hostname and path, you are probably reducing the limit to about 1k by encoding every character.)
 
gchrmt4:
(You are still very unlikely to hit it in real life... but if you are using Wininet then the limit is more like 2k: http://stackoverflow.com/a/3208930. In practice, allowing for "static" elements such as the hostname and path, you are probably reducing the limit to about 1k by encoding every character.)


Yes, it is wininet. But 2k is still far enough to be hit.

Edit: I just performed a test, and a string of length 3M has been encoded, sent from MQL4 and returned from server to MQL4 using the standard wininet http post. So I am not sure if any limit applies to the post except the php limit. Definitively I am not aware of some 1k limit now. I think the limit only applies to the url alone but not the post data included.

In addition I got a proof that strings >3M are working in MQL4 :)

 
Ex Ovo Omnia #:

I could not wait, here is what I used, seems to work.

A slightly edited version in MQL4, by skipping alpha numeric characters.

string urlencode(string s) {
   int len = StringLen(s);
   string encoded;
   uchar chars[];
   StringToCharArray(s,chars);
   for (int i = 0; i<len ;i++) {
      if (('a' <= chars[i] && chars[i] <= 'z') || ('A' <= chars[i] && chars[i] <= 'Z') || ('0' <= chars[i] && chars[i] <= '9')) {
         encoded = encoded + CharToStr(chars[i]);
      } else {
         encoded = encoded + StringFormat("%%%02X", chars[i]);
      }
   }
   return encoded;
}
Reason: