0

The app receives request which looks like(http://127.0.0.1:8000/?type%3DGCPrint%26get%3DPaperNames%3A%E9%BB%98%E8%AE%A4%E6%89%93%E5%8D%B0%E6%9C%BA) from client's XMLHttpRequest. The url is encoded by encodeURI() or encodeURIComponent() with javascript.
How can I get the canonical form url correctly?
I have reached the sdk and followed a demo, but the result is not correct, the last part of url is chinese characters.
Here is my test code

void UrlDecode()
{
    wstring url = L"http://127.0.0.1:8000/?type%3DGCPrint%26get%3DPaperNames%3A%E9%BB%98%E8%AE%A4%E6%89%93%E5%8D%B0%E6%9C%BA";

    DWORD bufSize = url.size()+1;
    wstring buffer(url.size()+1, L'\0');

    HRESULT hr = UrlCanonicalize(url.c_str(), &buffer[0], &bufSize, URL_UNESCAPE|URL_ESCAPE_UNSAFE);

    if (SUCCEEDED(hr))
    {
        wcout.imbue(locale(""));
        wcout<<buffer;
    }   
}

int _tmain(int argc, _TCHAR* argv[])
{
    UrlDecode();    
    //system("pause");  
    return 0;
}

And the result is
result result

o0ops
  • 91
  • 1
  • 9
  • tried with online tools http://www.url-encode-decode.com/ they also report chineese and junk characters – RC Brand Dec 30 '14 at 11:14
  • This problem has already been solved before, albeit not with the function you present - `UrlCanonicalize`. See either of the following links: http://stackoverflow.com/questions/8790610/how-to-mimic-the-js-decodeuricomponent-in-c or http://stackoverflow.com/questions/154536/encode-decode-urls-in-c – enhzflep Dec 30 '14 at 12:09

1 Answers1

0

@RC Brand @enhzflep thanks for your help.
I have tried UriParser, but there are some Encoding problems either.
At last I find a code snippnet, it works for me.
Firstly, convert the url to MultiByte string(murl) with CP_UTF8.
Then, decode murl with function urldecode, get decoded url durl.
Finally, Convert durl to WideChar with CP_UTF8.

o0ops
  • 91
  • 1
  • 9