gpt4 book ai didi

json - Delphi HTTP 发布 JSON

转载 作者:行者123 更新时间:2023-12-03 14:41:49 29 4
gpt4 key购买 nike

我确信我在这里做错了什么。我已经遵循了在 stackoverflow 上找到的所有示例,但仍然没有让它在我的环境中工作。我很想更新我的控件和环境,但我目前被我所拥有的所束缚。

我正在使用:

  • 德尔福7
  • 印地 10.0.52
  • ulkJSON.pas v1.07

我需要将此 JSON 发送到 URL:

"auth": {
"applicationId": "appID",
"applicationPassword": "pwd",
"accountId": "acct",
"userId": "dev"
}

这并没有什么特别疯狂的地方,但是当我尝试发布我的请求时,我往往会收到一条消息,表明该请求已正常关闭。 IDSocketHandle.pas 中的 CheckIsReadable 的 Handleallocated = false。我不确定我在配置 IdHTTP 时做错了什么,但它就是行不通。

我已经尝试过所有这些问题和其他几个问题的示例,但这些方法似乎都不适合我:

任何提示将不胜感激。

当前的变体如下所示:

procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);
var
code: Integer;
sResponse: string;
JsonToSend: TStringStream;
begin
JsonToSend := TStringStream.Create(
'{"auth": {"applicationId": "' + edApplication.text +
'","applicationPassword": "' + edpassword.text +
'","accountId": "' + edaccount.text +
'","userId": "' + edUser.text +
'"}}');
try
HTTP1.Request.ContentType := 'application/json';
HTTP1.Request.ContentEncoding := 'utf-8';

memoRequest.lines.clear;
memoRequest.lines.add(JsonToSend);

try
sResponse := HTTP1.Post(cbAddress.text, JsonToSend);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end;

memoResponse.lines.clear;
memoresponse.lines.add(sResponse);
finally
JsonToSend.Free();
end;
end;

idHTTP 组件当前设置如下:

object HTTP1: TIdHTTP
IOHandler = IdSSLIOHandlerSocketOpenSSL1
AuthRetries = 0
AuthProxyRetries = 0
AllowCookies = True
HandleRedirects = True
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentEncoding = 'utf-8'
Request.ContentLength = -1
Request.ContentRangeEnd = 0
Request.ContentRangeStart = 0
Request.ContentRangeInstanceLength = 0
Request.ContentType = 'application/json'
Request.Accept = 'application/json'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
HTTPOptions = [hoForceEncodeParams]
Left = 564
Top = 120
end

最佳答案

HTTP1.Request.ContentEncoding 应改为 HTTP1.Request.CharSet。 UTF-8 是一种字符集编码,而不是内容编码。然后在发布之前确保您的 JSON 数据实际上已编码为 UTF-8。如果您使用 ASCII 字符,则您显示的 TStringStream 代码就可以了。但如果您使用非 ASCII 字符,则需要对其进行编码,例如使用 Utf8Encode()TIdHTTP 不会对 TStream 数据进行编码,而是按原样发送。

Procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);
var
Json: string;
sResponse: string;
JsonToSend: TStringStream;
begin
Json := '{"auth": {"applicationId": "' + edApplication.text +
'","applicationPassword": "' + edpassword.text +
'","accountId": "' + edaccount.text +
'","userId": "' + edUser.text +
'"}}';

memoRequest.Text := Json;

JsonToSend := TStringStream.Create(Utf8Encode(Json)); // D2007 and earlier only
//in D2009 and later, use this instead:
//JsonToSend := TStringStream.Create(Json, TEncoding.UTF8);
try
HTTP1.Request.ContentType := 'application/json';
HTTP1.Request.CharSet := 'utf-8';

try
sResponse := HTTP1.Post(cbAddress.Text, JsonToSend);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end;
finally
JsonToSend.Free;
end;

memoResponse.Text := sResponse;
end;

或者:

Procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);
var
Json: string;
sResponse: string;
JsonToSend: TMemoryStream;
begin
Json := '{"auth": {"applicationId": "' + edApplication.text +
'","applicationPassword": "' + edpassword.text +
'","accountId": "' + edaccount.text +
'","userId": "' + edUser.text +
'"}}';

memoRequest.Text := Json;

JsonToSend := TMemoryStream.Create;
try
WriteStringToStream(JsonToSend, Json, enUTF8);
JsonToSend.Position := 0;

HTTP1.Request.ContentType := 'application/json';
HTTP1.Request.CharSet := 'utf-8';

try
sResponse := HTTP1.Post(cbAddress.Text, JsonToSend);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end;
finally
JsonToSend.Free;
end;

memoResponse.Text := sResponse;
end;

关于json - Delphi HTTP 发布 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24025646/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com