- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我如何使用 IdHTTP
将消息作为 PostMan
发送如下:
我的第一次尝试如下:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TStringStream;
Response : string;
LMsg : string;
begin
Result := False;
LMsg := '-----------------------------13932'+
'Content-Type: application/json; charset=utf-8'+
'Content-Description: message'+ sLineBreak+ '{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}'+
'-----------------------------13932--;'+sLineBreak;
Params := TStringStream.Create(LMsg, TEncoding.UTF8);
try
IdHTTP.Request.CustomHeaders.AddValue('authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
IdHTTP.Request.AcceptLanguage := 'Accept-Language';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
Params.Position := 0;
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
第二次尝试我这样尝试
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TStringStream;
Response : string;
LMsg : string;
begin
Result := False;
LMsg := '{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}';
Params := TStringStream.Create(LMsg, TEncoding.UTF8);
try
IdHTTP.Request.CustomHeaders.AddValue('authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.CustomHeaders.AddValue('Content-Description', 'message'); // I addedd this as on PostMan Body
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
IdHTTP.Request.AcceptLanguage := 'Accept-Language';
IdHTTP.Request.ContentType := 'application/json; charset=utf-8'; // I alos changed this as it shown on PostMan body
Params.Position := 0;
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
两次尝试都返回 HTTP/1.1 400 Bad Request
。
有什么建议我做错了吗?
最佳答案
在您的第一个示例中,您的“原始”MIME 数据格式不正确:
您缺少一堆必需的换行符。并且不要使用 sLineBreak
常量,因为它的值是特定于平台的。 MIME 期望换行符专门使用 CRLF
。 Indy 有一个用于该值的 EOL
常量。
您在结束边界线的末尾有一个错误的分号。
您也没有正确设置 Request.AcceptEncoding
属性。不要手动启用编码,除非您准备好在响应中实际手动处理它们(您的代码不是)。 TIdHTTP
为您处理 gzip
和 deflate
编码,如果您分配一个 TIdZLibCompressorBase
派生组件,例如 TIdCompressorZLib
,到 TIdHTTP.Compressor
属性。不要担心 br
编码,它没有被广泛使用。简而言之,将 Request.AcceptEncoding
保留为默认值,让 TIdHTTP
为您管理它。
您也没有正确设置 Request.AcceptLanguage
属性。您应该将其设置为 'en-US,en;q=0.8'
,而不是 'Accept-Language'
。
如果您进行了这些修复,您的第一个示例应该可以工作,例如:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TStringStream;
Response : string;
LMsg : string;
begin
Result := False;
LMsg := '-----------------------------13932' + EOL +
'Content-Type: application/json; charset=utf-8' + EOL +
'Content-Description: message' + EOL +
EOL +
'{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}' + EOL +
'-----------------------------13932--' + EOL;
Params := TStringStream.Create(LMsg, TEncoding.UTF8);
try
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptLanguage := 'en-US,en;q=0.8';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
或者:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TMemoryStream;
Response : string;
LMsg : string;
begin
Result := False;
Params := TMemoryStream.Create;
try
WriteStringToStream(Params, '-----------------------------13932' + EOL);
WriteStringToStream(Params, 'Content-Type: application/json; charset=utf-8' + EOL);
WriteStringToStream(Params, 'Content-Description: message' + EOL);
WriteStringToStream(Params, EOL);
WriteStringToStream(Params, '{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}' + EOL, IndyTextEncoding_UTF8);
WriteStringToStream(Params, '-----------------------------13932--' + EOL);
Params.Position := 0;
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptLanguage := 'en-US,en;q=0.8';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
或者:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TMemoryStream;
Response : string;
LMsg : string;
begin
Result := False;
Params := TMemoryStream.Create;
try
with TStreamWriter.Create(Params, TEncoding.UTF8) do
try
NewLine := EOL;
WriteLine('-----------------------------13932');
WriteLine('Content-Type: application/json; charset=utf-8');
WriteLine('Content-Description: message');
WriteLine;
WriteLine('{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}');
WriteLine('-----------------------------13932--');
finally
Free;
end;
Params.Position := 0;
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptLanguage := 'en-US,en;q=0.8';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
在您的第二个示例中,您的“原始”数据本身就是 JSON,而不是任何包装它的 MIME。您将 MIME header 放在它们不属于的 HTTP header 中。如果服务器需要 MIME 数据而不仅仅是原始 JSON 数据,则此示例将不起作用。
您在 Request.AcceptEncoding
和 Request.AcceptLanguage
属性上也犯了同样的错误。
由于您以 MIME 格式发布数据,处理此问题的更简单方法本来是改为使用 Indy 的 TIdMultipartFormDataStream
类,并让它处理 MIME 格式你。但是,该类目前不支持:
将流的 RequestContentType
属性设置为自定义值(在本例中,'multipart/mixed'
而不是 'multipart/form-data '
).不过,您可以使用访问器类来完成此操作,因为 FRequestContentType
成员是 protected
。
省略个别字段上的 Content-Disposition: form-data
header 。这可能会使不需要 form-data
提交的服务器出错。
完全指定 Content-Description
MIME header (请参阅 GitHub 上 Indy 问题跟踪器中的 Add support for user-defined MIME headers in TIdMultipartFormDataStream)。
因此您将不得不继续手动设置 MIME 数据的格式。你只需要确保你做对了。
关于http - IdHTTP 如何发送原始正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44665349/
我正在准备申请。 IDHTTP:使用Get方法。但是有一些问题。我要创建一个代理列表,然后单击添加IDHTTP的列表框项(代理地址)。对不起,我不太会英语。 我的密码; procedure TForm
我正在尝试使用 Indy HTTP 类进行 HTTP 身份验证。但由于某种未知的原因,我在这一行中收到了访问冲突错误:IdHTTP1.Request.Authentication.Username :
我正在使用 idhttp (Indy) 进行一些网站检查。我想要它做的就是在发送请求后检查服务器的响应代码,我不想实际上必须从服务器接收 HTML 输出,因为我只监视 200 OK 代码,任何其他代码
请向我解释如何使用 SSL (https://) 从服务器下载文件。我在互联网上没有找到合适的答案。 每个人都在谈论 TIdSSLIOHandlerSocket,但我只有 TIdSSLIOHandle
请向我解释如何使用 SSL (https://) 从服务器下载文件。我在互联网上没有找到合适的答案。 每个人都在谈论 TIdSSLIOHandlerSocket,但我只有 TIdSSLIOHandle
我如何使用 IdHTTP 将消息作为 PostMan 发送如下: 我的第一次尝试如下: function TIdFoo.SendIM(const AID, AMessage: string): Boo
印地 10: 两个线程,线程 1 正在调用 TIdHTTP 上的 Get 并阻止读取数据。线程2将在同一个TIdHTTP上调用disconnect以中断Get。 我在 TIdHTTP 上使用摘要式身份
使用:Delphi 2010,最新版本的 Indy 我正在尝试从 Google Adsense 网页上抓取数据,目的是获取报告。然而到目前为止我还没有成功。它在第一个请求后停止并且不再继续。 使用 F
我使用 TIdHttp 来获取网页内容。响应头指示内容编码为utf8。我想在控制台中打印内容为CP936(简体中文),但实际内容不可读。 Result := TEncoding.Utf8.GetStr
一家公司要求我记录我们的客户端仅在软件级别通过 TCP 协议(protocol)与我们的服务器进行通信。我一直假设 IdHTTP 使用 TCP,但我不确定如何证明它。我在文档中找不到它。 最佳答案 T
我已指定 OnStatus 处理程序使用 AStatusText 参数更新标签。 在处理程序中设置断点时,它只会在我执行 Get("google.com") 时在 hsConnected 状态下中断。
像这样使用 TIdHttp 时: Memo1.Text := IdHTTP1.post(url,data); 如果 memo1 没有给出 http 错误,我可以获得响应内容。但是当它给出 http 错
好的,我已经动态创建了 Idhttp,如下所示 procedure TForm1.Button1Click(Sender: TObject); Var Resp : String; begin
我发现,当为 TIdHTTP 组件设置 ConnectTimeoout 属性时,它会使请求(GET 和 POST)变慢约 120 毫秒? 这是为什么,我可以以某种方式避免/绕过它吗? Env:D201
如何检查特定响应代码(例如 200 OK)的目标 URL,而 Indy 不会抛出各种异常。连接超时、连接正常关闭等... 例如,如果 URL 不正确或者找不到或无法访问其主机。即使我试图忽略它们,In
我目前有这个 TThread 将一些图像下载到桌面,这是线程代码: type TDownloadUpdateAnimateEvent = procedure(Sender: TObject; AA
我正在尝试运行此代码: var objHttp: TIdHTTP; ... objHttp.HandleRedirects := True; objHttp.AllowCookies :=
我正在尝试使用 TIdHTTP 上传文件。问题是当请求发送到服务器时访问 token 会发生更改。 我使用的访问 token 是fJNhDM6TlcpeVmD8h3jFuPJS71sxwZB8bZBX
主要目标是将所有数据发送到远程 mysql 数据库。我使用 TidHTTP 作为 POST 方法。拥有近 10.000 条记录,约 2MB 数据。当我运行delphi代码时,上传json数据。但有些记
我使用 IdHTTP.get 来获取 url 的 HTMl,但是在这个 url 中是一个用 ajax 加载的表,我必须等待几秒钟才能加载它,然后才能获取页面的 HTML。 我可以等一下吗? 最佳答案
我是一名优秀的程序员,十分优秀!