使用 Embarcadero C++ TRESTRequest 时如何包含端口号

How do you include a port number when using Embarcadero C++ TRESTRequest

这是强制性代码片段:

UnicodeString SeizonshaRest::doStuff(UnicodeString strEmail, UnicodeString strPassword)
{
    restclient->BaseURL = "http://localhost";
    restrequest->Resource = ":60800/home/login";
    restrequest->Params->AddItem("email", strEmail);
    restrequest->Params->AddItem("password", strPassword);
    UnicodeString strId;
    restrequest->Execute();
    strId = respMain->Content;
    return strId;
}

我知道这是错误的做法。 请问添加端口号的正确方法是什么?

端口号附加到客户端连接的服务器主机名,而不是客户端连接后请求的资源。

试试这个:

restclient->BaseURL = "http://localhost:60800";
restrequest->Resource = "home/login";

如果您阅读文档,它会说:

TCustomRESTClient.BaseURL

Specifies the base URL for all API calls.

All resources and parameters of your requests will be appended to this URL. Please, be aware that a trailing forward slash ("/") is added to the value of the BaseURL property.

TCustomRESTRequest.Resource

This property is added to the base URL to establish a complete URL for the HTTP request.

Important: The Resource value should meet the following limitations:

  • Does not include the scheme or domain mame.
  • Does not include the leading slash.

因此,您的原始代码会将请求 URL 生成为 http://localhost/:60800/home/login,但正确的 URL 是 http://localhost:60800/home/login

我真的很想 post 我的代码,因为我在任何地方都找不到这样的例子

contact.h:

#ifndef contactH
#define contactH
#include <System.Classes.hpp>
#include <IPPeerClient.hpp> 
#include <REST.Client.hpp>
//---------------------------------------------------------------------------
class contact;

class contact
{
   public:
      contact();
      ~contact() {};
      String connect(String query);
      TRESTClient *clientContact;
      TRESTRequest *reqContact;
      TRESTResponse *respContact;
   private:
};

contact.cpp:

String contact::connect(String query)
{
    clientContact = new TRESTClient(NULL);
    reqContact = new TRESTRequest(NULL);
    respContact = new TRESTResponse(NULL);
    reqContact->Client = clientContact;
    reqContact->Response = respContact;
    UnicodeString strUrl = "http://myawesomeurl:5150";
    reqContact.Resource = query;
    clientContact->BaseURL = strUrl;
    UnicodeString strInspect;
    reqContact->Execute();
    strInspect = respContact->Content;
    return strInspect;
}

我希望这能帮助像我一样陷入困境的人。