带有Unicode参数的C ++ Builder TRestRequest

时间:2015-02-26 08:50:28

标签: delphi c++builder c++builder-xe5

我正在使用TRestRequest从服务器获取数据。我需要用Unicode字符串值填充参数:“ôpen”。但是,我在使用此Unicode字符串作为查询参数时调用Execute崩溃。

我的代码:

    RESTRequest->ResetToDefaults();
    RESTRequest->AddParameter("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8 ", TRESTRequestParameterKind::pkHTTPHEADER);
    //  Get Indexing Status
    RESTRequest->Resource = "XXX/collection1"+ Form1->teReuqestHandler->Text+"?";
    RESTRequest->Method = rmGET;
    // replace all space in name field with '\ '
    UnicodeString lcQuery = Form1->teQuery->Text; // this value should be support french language or ...
    // Body
    RESTRequest->AddParameter("q",lcQuery, TRESTRequestParameterKind::pkGETorPOST);
    // Run
    String str1 = RESTRequest->GetFullRequestURL();
    RESTRequest->Execute(); // here when pass "ôpen" to lcQuery, it crash

如何正确地将“ôpen”添加到我的网址?

1 个答案:

答案 0 :(得分:0)

Content-Type: application/x-www-form-urlencoded; charset=UTF-8 HTTP标头在GET请求中没有意义,仅在POST请求中。

Embarcadero的REST框架无法正确处理非ASCII字符集。即使它在内部使用Indy,它也不会使用Indy的charset处理。因此,为了发送UTF-8编码数据,您必须:

  1. 手动将UnicodeString编码为UTF-8,然后将UTF-8八位字节重新放回UnicodeString,以便TRESTRequest可以发送它们:

    UnicodeString EncodeAsUtf8(const UnicodeString &s)
    {
        UTF8String utf8 = s;
        UnicodeString ret;
        ret.SetLength(utf8.Length());
        for (int x = 1; x <= utf8.Length(); ++x)
            ret[x] = (WideChar) utf8[x];
        return ret;
    }
    
    ...
    
    RESTRequest->ResetToDefaults();
    RESTRequest->Method = rmGET;
    RESTRequest->Resource = L"XXX/collection1" + Form1->teReuqestHandler->Text;
    RESTRequest->AddParameter(L"q", EncodeAsUtf8(Form1->teQuery->Text), TRESTRequestParameterKind::pkGETorPOST);
    String str1 = RESTRequest->GetFullRequestURL();
    RESTRequest->Execute();
    
  2. 自己编码参数数据:

    #include <IdGlobal.hpp>
    #include <IdURI.hpp>
    
    RESTRequest->ResetToDefaults();
    RESTRequest->Method = rmGET;
    RESTRequest->Resource = L"XXX/collection1" + Form1->teReuqestHandler->Text;
    RESTRequest->AddParameter(L"q", TIdURI::ParamsEncode(Form1->teQuery->Text, IndyTextEncoding_UTF8), TRESTRequestParameterKind::pkGETorPOST, TRESTRequestParameterOptions() << TRESTRequestParameterOption::poDoNotEncode);
    String str1 = RESTRequest->GetFullRequestURL();
    RESTRequest->Execute();
    

    或者:

    #include <IdGlobal.hpp>
    #include <IdURI.hpp>
    
    RESTRequest->ResetToDefaults();
    RESTRequest->Method = rmGET;
    RESTRequest->Resource = L"XXX/collection1" + Form1->teReuqestHandler->Text + L"?q={q}";
    RESTRequest->AddParameter(L"q", TIdURI::ParamsEncode(Form1->teQuery->Text, IndyTextEncoding_UTF8), TRESTRequestParameterKind::pkURLSEGMENT, TRESTRequestParameterOptions() << TRESTRequestParameterOption::poDoNotEncode);
    String str1 = RESTRequest->GetFullRequestURL();
    RESTRequest->Execute();
    
  3. 否则,切换到Indy的TIdHTTP组件:

    UnicodeString Response = IdHTTP1->Get(L"http://server/XXX/collection1" + Form1->teReuqestHandler->Text + L"?q=" + TIdURI::ParamsEncode(Form1->teQuery->Text, IndyTextEncoding_UTF8));