我正在开发一个工具来集成两个系统,我需要一些有关 Delphi 和 Webservices 的帮助。
首先,我正在使用 Delphi 5.0 和 Indy 8.009B。我知道它们都很古老,但这就是我现在必须处理的问题。
我遇到的第一个问题是关于检索 token 。但我通过找到正确的 DLL 文件来与我的 Indy 的 IdHTTP 组件版本(顺便说一下,2004 年的版本)配合使用,成功地做到了这一点。
这是我的代码:
slParams := TStringList.Create;
sReturnWS := TStringStream.Create('');
cIntercept := TIdConnectionInterceptOpenSSL.Create(nil);
sParams := 'client_id=' + sIdClient + '&client_secret=' + sClientKey;
sURL := sUrlAut + '?' + sParams;
IdHTTP := TIdHTTP.Create(nil);
IdHTTP.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
cIntercept.SSLOptions.Method := sslvTLSv1;
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP.Request.ContentEncoding := 'utf-8';
IdHTTP.Intercept := cIntercept;
IdHTTP.ProtocolVersion := pv1_0;
IdHTTP.Request.Connection := 'keep-alive';
IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
try
IdHTTP.Post(sURL, slParams, sReturnWS);
sReturn := sReturnWS.DataString;
vJson := TlkJSON.ParseText(sReturn);
它就像一个魅力。但现在我目前陷入了第二步,即通过不记名 token 授权/身份验证向服务器发送收到的 token 。我动态地创建了要发送到服务器的 json,顺便说一下,这是一个 HTTPS 安全服务器。 json 文件很好,我已经通过将其从内存流保存到硬盘驱动器来使用在线 json 验证器对其进行了测试,并且我还使用 Postman 和 SoapUI 对其进行了测试,并且它可以正常工作并得到验证。但是当我尝试通过我的 Delphi 应用程序发送它时,我总是收到相同的错误: 422 Unprocessable Entity 这让我发疯,因为这就是我可以从服务器响应中读取到的全部内容。它不像 Postman 或 SoapUI 那样我可以读取服务器返回的所有消息。好吧,我相信我遇到这个错误是因为我没有通过 Delphi 正确设置一些必要的授权;不管怎样,这是我发送 json 文件的代码(json 是我包含 json 数据的字符串变量):
JsonToSend.WriteBuffer(Pointer(Json)^, Length(Json));
JsonToSend.Position := 0;
cIntercept := TIdConnectionInterceptOpenSSL.Create(nil);
try
IdHTTPAux.Intercept := cIntercept;
IdHTTPAux.Request.Clear;
IdHTTPAux.Request.ContentType := 'application/json';
IdHTTPAux.Request.ContentEncoding := 'UTF-8';
IdHTTPAux.Request.ExtraHeaders.Clear;
IdHTTPAux.Request.ExtraHeaders.FoldLines := False;
IdHTTPAux.Request.WWWAuthenticate := 'Bearer TOKEN';
idHTTPAux.Request.ExtraHeaders.Values['Cache-Control'] := 'no-cache';
IdHTTPAux.Request.ExtraHeaders.Values['Authorization'] :=
Uppercase(copy(sTokenType,1,1)) + copy(sTokenType,2,length(sTokenType)-1) + ' ' +
sToken;
IdHTTPAux.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
cIntercept.SSLOptions.Method := sslvTLSv1;
IdHTTPAux.Request.Accept := '*/*';
IdHTTPAux.ProtocolVersion := pv1_0;
IdHTTPAux.Request.Connection := 'Keep-Alive';
IdHTTPAux.Request.AcceptEncoding := 'gzip, deflate, br';
try
IdHTTPAux.Post(sUrl, JsonToSend, sResponse);
请有人帮助我,因为我已经被困在这个问题上好几天了,我已经尝试了我力所能及的一切,但我不知道该怎么办了。
最佳答案
我解决了。事实上,在验证动态生成的 json 后的某个时刻,我不小心删除了其中的最后一个“}”字符。代码本身没问题。现在 100% 正常工作。因此,如果有人需要一个关于如何使用 Delphi 5 和 Indy 8.009B 使用 json 和 webservice 集成系统的示例,那就是。它有效!
关于json - Delphi 和网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67624832/