Delphi C++;带Unicode参数的生成器TRestRequest

Delphi C++;带Unicode参数的生成器TRestRequest,delphi,c++builder,c++builder-xe5,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 ", TRESTRequest

我正在使用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”添加到我的URL?

内容类型:application/x-www-form-urlencoded;charset=UTF-8HTTP头在
GET
请求中没有意义,只有在
POST
请求中才有意义

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

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

    UnicodeString编码asutf8(const UnicodeString&s)
    {
    utf8字符串utf8=s;
    独角兽;
    ret.SetLength(utf8.Length());
    对于(int x=1;x ResetToDefaults();
    RESTRequest->Method=rmGET;
    RESTRequest->Resource=L“XXX/collection1”+Form1->teReuqestHandler->Text;
    RESTRequest->AddParameter(L“q”,编码asutf8(Form1->teQuery->Text),TRESTRequestParameterKind::pkGETorPOST);
    字符串str1=RESTRequest->GetFullRequestURL();
    RESTRequest->Execute();
    
  • 自己对参数数据进行编码:

    #包括
    #包括
    RESTRequest->ResetToDefaults();
    RESTRequest->Method=rmGET;
    RESTRequest->Resource=L“XXX/collection1”+Form1->teReuqestHandler->Text;
    RESTRequest->AddParameter(L“q”,TIdURI::ParamsEncode(Form1->teQuery->Text,indytextencode_UTF8),TRESTRequestParameterKind::pkGETorPOST,TRESTRequestParameterOptions()GetFullRequestURL();
    RESTRequest->Execute();
    
    或:

    #包括
    #包括
    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,indytextencode_UTF8),TRESTRequestParameterKind::pkurlsem分段,TRESTRequestParameterOptions()GetFullRequestURL();
    RESTRequest->Execute();
    
  • 否则,切换到Indy的
    TIdHTTP
    组件:

    UnicodeString Response=IdHTTP1->Get(L)http://server/XXX/collection1“+Form1->teReuqestHandler->Text+L”?q=“+TIdURI::ParamsEncode(Form1->teQuery->Text,IndyTextEncoding_UTF8));
    
    您应该使用UTF-8对URL参数进行编码:Utf8Encode无法解决此问题!请使用
    UrlEncode(Utf8Encode(Text))
    。您还没有指定使用哪个版本的c++builder。文本是unicode(WideString)环境是NaveCabeRo C++ + XE5…TeXTE是UNIODESCOPE…这是告诉你,你需要指定属性。它与PARAMS或编码无关。你确定资源是否完整?我还没有使用TrStruTestObjor,但是对于代码来说,你似乎缺少查询的URL本身,这是哪个?包括直接使用Indy的协议(HTTP等)似乎是迄今为止最简单的选择,很难相信Embarcadero在2015年仍然推出了非Unicode支持的API