我相信我在这里做错了.我已经跟踪了我在stackoverflow上找到的每个例子,但仍然没有在我的环境中使用它.我想更新我的控件和环境,但我现在已经锁定了我拥有的东西.
我在用:
我需要将此JSON发送到URL:
"auth": {
"applicationId": "appID",
"applicationPassword": "pwd",
"accountId": "acct",
"userId": "dev"
}
Run Code Online (Sandbox Code Playgroud)
这没有什么可怕的,但当我尝试发布我的请求时,我倾向于收到一条消息,请求是优雅地关闭.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;
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Rem*_*eau 10
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;
Run Code Online (Sandbox Code Playgroud)
或者:
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43675 次 |
| 最近记录: |