我确信我在这里做错了什么。我已经跟踪了我在stackoverflow上找到的每个例子,但仍然无法在我的环境中使用它。我喜欢更新我的控件和环境,但我现在已经锁定了我拥有的内容。
我正在使用:
我需要将此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
答案 0 :(得分: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;
可替换地:
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;
答案 1 :(得分:1)
请试试这个:
procedure TForm1.Button1Click(Sender: TObject);
var
s: String;
Resp_Json: string;
Req_Json:TStream;
begin
s:='state=1';
s:=s+'&kind=0';
s:=s+'&tblid=0';
Req_Json:=TstringStream.Create(s);
Req_Json.Position:=0;
try
IdHTTP1.Request.UserAgent:='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36';
IdHTTP1.Request.Accept := 'application/json, text/javascript, */*; q=0.01';
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded; charset=UTF-8';
IdHTTP1.Request.CharSet:='utf-8';
Resp_Json:=IdHTTP1.Post('http://[your URL]', Req_Json);
finally
Req_Json.Free;
end;
memo1.Lines.Add(IdHTTP1.ResponseText);
memo1.Lines.Add(Resp_Json);
end;