Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

how to specify the CodePage of uri encode

Moderators: larsivi kris

Posted: 09/23/07 03:37:44 Modified: 09/23/07 03:38:57

some server not accept UTF-8 format uri encode. here is a simple solution with Phobos:

string urlEncode(string url, int codepage = 0)
{
    string result;
    try
    {
        string esc = 
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;/?:@&=+$,-.!~*'()";
        //RFC2396 Mark characters
        foreach(v; toMBS(url, codepage))
        {
            if (v == ' ') 
                result ~= "%20";  // + ?
            else if (std.string.find(esc, v) > -1) 
                result ~= v;
            else if ( v < 0x80 )
                result ~= std.string.format("%%%2x", v);                
            else
                result ~= std.string.format("%%%2x", v);
        }
    }
    catch(Exception e)
    {
        printf("urlEncode catch %.*s\n",e.msg);
    }
    return result;
}
//

How to do that with tango.net.Uri?

Author Message

Posted: 09/25/07 00:21:41

I'm not sure exactly what you need, but URI is supposed to be UTF8 only :)

You might find something useful within tango.sys.win32.CodePage ?