简体   繁体   English

使用Indy调用Datasnap服务器的内部服务器错误

[英]Internal Server Error calling a Datasnap server using Indy

I have set up a very basic DataSnap Server Application (Delphi XE3), containing the 2 sample methods, EchoString and ReverseString. 我已经建立了一个非常基本的DataSnap Server应用程序(Delphi XE3),其中包含2个示例方法EchoString和ReverseString。 I have added authentication so that only if the user calling the method is called "standard", they have access to the ReverseString method. 我添加了身份验证,以便仅当调用该方法的用户称为“标准”时,他们才可以访问ReverseString方法。

procedure TServerContainer1.DSAuthenticationManager1UserAuthenticate(
Sender: TObject; const Protocol, Context, User, Password: string;
var valid: Boolean; UserRoles: TStrings);
begin
  valid := (User <> '');

  if (SameText(User, 'standard') = True) then
  begin
    UserRoles.Add('standard');
  end;
end;

type
TServerMethods1 = class(TDSServerModule)
private
  { Private declarations }
public
  { Public declarations }
  function EchoString(Value: string): string;
  [TRoleAuth('standard')]
  function ReverseString(Value: string): string;
end;

If I call this method from a browser directly, eg 如果我直接从浏览器中调用此方法,例如

http://localhost:8080/datasnap/rest/TServerMethods1/ReverseString/TestFromBrowser

then I get the expected response (after the browser's default login prompt, in which I enter an invalid user, eg Jason): 然后我得到预期的响应(在浏览器的默认登录提示后,我输入一个无效用户,例如Jason):

{"error":"jason is not authorized to perform the requested action."}

However, if I call it from a Delphi client application using Indy (TIdHTTP): 但是,如果我使用Indy(TIdHTTP)从Delphi客户端应用程序调用它:

IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := 'jason';
IdHTTP1.Request.Password := 'jason';

Label2.Caption := IdHTTP1.Get('http://localhost:8080/datasnap/rest/TServerMethods1/ReverseString/TestFromDelphi');

I get this response: 我得到这个回应:

HTTP/1.1 500 Internal Server Error

How can I avoid the error and receive the same RESTful response that I got in the browser? 如何避免该错误并获得与浏览器相同的RESTful响应? I have been trying to figure out how to view the HTTP request sent by the browser vs that sent by Indy but haven't managed it, even using Ethereal. 我一直在试图弄清楚如何查看浏览器发送的HTTP请求与Indy发送的HTTP请求,但是即使使用Ethereal,也无法对其进行管理。

I have been able to establish what is going on. 我已经能够确定发生了什么。 Using Wireshark and Rawcap (to capture local loopback requests), I can see that even though the browser is showing the meaningful message: 使用Wireshark和Rawcap(捕获本地回送请求),即使浏览器显示有意义的消息,我也可以看到:

{"error":"jason is not authorized to perform the requested action."}

it is also returning an HTTP 500 Internal Server Error. 它还返回HTTP 500内部服务器错误。 However, the meaningful response text is also returned as part of this response, which is being displayed by the browser. 但是,有意义的响应文本也作为该响应的一部分返回,正在由浏览器显示。

来自浏览器的响应

Checking the Indy HTTP response, it is the same; 检查Indy HTTP响应,是否相同。 the actual response code is a 500, but the text I want to display is also being returned. 实际的响应代码是500,但是我要显示的文本也被返回。

在此处输入图片说明

This is accessible by capturing the EIdHTTPProtocolException and accessing its ErrorMessage property. 可通过捕获EIdHTTPProtocolException并访问其ErrorMessage属性来访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM