IdHTTP 如何发送原始正文

2024-01-28

我如何使用IdHTTP发送消息为PostMan下面的dos:

我的第一次尝试如下:

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具体来说。印地有一个EOL该值的常数。

  • 结束边界线末尾有一个错误的分号。

您也没有设置Request.AcceptEncoding属性正确。不要手动启用编码,除非您准备在响应中实际手动处理它们(您的代码没有)。TIdHTTP把手gzip and deflate为您编码,如果您分配TIdZLibCompressorBase-派生组件,例如TIdCompressorZLib,到TIdHTTP.Compressor财产。不用担心br编码,目前应用并不广泛。简而言之,留下Request.AcceptEncoding以其默认值并让TIdHTTP为您管理。

您也没有设置Request.AcceptLanguage属性正确。你应该将其设置为'en-US,en;q=0.8',不至于'Accept-Language'.

你的第一个例子should如果您进行这些修复,则可以工作,例如:

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 标头放入 HTTP 标头中,而它们不属于这些标头。如果服务器需要 MIME 数据而不仅仅是原始 JSON 数据,则此示例将不起作用。

你也在犯同样的错误Request.AcceptEncoding and Request.AcceptLanguage特性。


由于您以 MIME 格式发布数据,因此有一种更简单的方法来处理此问题本来可以使用印地TIdMultipartFormDataStream类,并让它为您处理 MIME 格式。但是,该类当前不支持:

  • 设置流的RequestContentType属性为自定义值(在本例中,'multipart/mixed'代替'multipart/form-data')。不过,您可以使用访问器类来完成此操作,因为FRequestContentType成员是protected.

  • 省略Content-Disposition: form-data各个字段的标题。这可能会导致意外的服务器崩溃form-data意见书。

  • 指定Content-DescriptionMIME 标头(参见在 TIdMultipartFormDataStream 中添加对用户定义的 MIME 标头的支持 https://github.com/IndySockets/Indy/issues/172在 GitHub 上的 Indy 问题跟踪器中)。

因此,您必须继续手动格式化 MIME 数据。你只需要确保你做对了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IdHTTP 如何发送原始正文 的相关文章

随机推荐

  • 尝试在Elasticsearch中设置max_gram和min_gram

    我尝试在 Ubuntu 16 04 EC2 服务器上部署 Ruby on Rails 应用程序 但给出了有关 Elasticsearch 上 max gram 和 min gram 之间差异的错误 我还没有任何 Elasticsearch
  • React:将双色滑块拇指设置为具有透明背景的图像

    我制作了一个滑块 左边应该是蓝色 右边是灰色 蓝色也应该比灰色更厚 我有两个问题 如果拇指片具有透明切口 则滑块的蓝色部分不会一直延伸到图像上可见内容的左侧 我希望滑块的边缘是圆形的 尤其是蓝色边缘 我可能不需要灰色 By round ed
  • 按下按钮时触发点击事件

    我正在创建一个将元素移动到右侧的指令 每次单击时 该项目都会移动到右侧 但我希望只要按住按钮 该元素就会移动 directive car function return restrict A link function scope elem
  • Selenium Webdriver - 获取表数据

    我想从 UI 中的表中获取数据 我知道使用 tr 和 td 循环行和列 但我的桌子是这样的 table tbody tr td data td th data th td data td td data td tr tr td data t
  • VS 2015 文件保存时 CPU 使用率较高

    使用 Visual Studio 2015 我注意到 如果我使用所有解决方案的通用项目打开多个解决方案 如果我编辑并保存属于通用项目的一个 cs 文件 则所有 Visual Studio 2015 实例将消耗 CPU 10 15秒 请注意
  • (很多)UIWebView 内存泄漏

    我从其他帖子中看到 UIWebView 存在内存泄漏问题 然而 由于我泄漏的对象数量很多 我不得不怀疑我是否做错了什么 Leaks 报告了关于打开 UIWebView 加载页面和关闭 这是 Facebook 登录页面 的大约 60 次泄漏
  • java进程中提交内存和RSS的区别

    我正在运行一个简单的 java 进程 该进程运行 jetty 其顶部显示 2 9g 的 RAM 使用的JDK版本是1 8 0 112 使用本机内存跟踪 jcmd 显示总提交内存仅为 1 5G 内存 正如 jvisualvm 所报告的 直接缓
  • 添加用户输入的数字并在用户输入“end”时中断的循环

    创建一个不需要参数的函数 该函数要求用户输入一系列大于或等于零的数字 一次一个 用户键入 end 表示不再有数字 该函数计算所有输入值的总和 我应该能够仅使用 while 循环和 if 语句来完成此操作 我遇到的主要问题是我不知道如何做到这
  • SQLite 查询根据另一个表的值获取表

    我不确定这里必须有什么标题才能正确反映我的问题 我只能描述我想要的内容 有一个包含字段的表 id name city 还有接下来的几行 1 John London 2 Mary Paris 3 John Paris 4 Samy Londo
  • 无法使用 Laravel Mix 复制文件夹

    我有一个项目使用拉拉维尔 5 4 and Laravel 混合 我有一个图像文件夹 其中一些图像具有相同的名称但类别不同 因此它们包含在不同的目录中 So resources assets images globalimage png ve
  • 由于共享库事件而停止 - Visual Studio Code

    我是 Visual Studio Code 的初学者 我尝试在其上调试我的 C 代码 我这里有一个示例代码 include iostream using namespace std int main cout lt lt hello wor
  • My.Settings 中自定义类的数组列表

    我有一个 Visual Basic Net 2 0 程序 我正在将设置从较旧的设置文件移至 app config 程序设置文件 我正在努力尽可能地做好这件事 所以 我添加了我的设置如图所示 https i stack imgur com e
  • 启动应用程序时使用 pg-promise 验证数据库连接

    我正在构建一个快速应用程序 它使用以下命令连接到 postgres 数据库pg 承诺 https github com vitaly t pg promise module 我想确保启动应用程序服务器时数据库连接成功 换句话说 如果与数据库
  • Android 在通知单击时打开特定选项卡片段

    我有一个使用操作栏选项卡的 Android 应用程序 还有一个通知系统 我想在单击通知时直接打开特定选项卡 如何做到这一点 因为通知挂起意图只能打开活动 而我的主要活动包含 3 个选项卡的 3 个片段 以下是选项卡主要活动的代码 publi
  • Ionic v2 按钮文本大写

    在我的 ionic v2 应用程序中有一个按钮 无论我输入哪个文本 它总是大写 我不想添加 css utilities 因为我混合了小写和大写单词 这是我的代码
  • SQLiteConstraintException 不要进入 catch 内部

    当我运行该代码时 db insert 内部出现异常 08 29 15 40 17 519 E SQLiteDatabase 3599 android database sqlite SQLiteConstraintException col
  • Mono .EXE 程序集集资源管理器图标

    在Linux上使用gmcs编译时 如何设置最终EXE将使用的资源管理器图标 我有一个 ico 文件要附加到输出 exe 答案必须是可自动化构建并在 Linux 上执行 无需 wine 构建机器架构不是 x86 可以使用针对 Windows
  • 将 XML 值反序列化为枚举时处理额外空格

    我一直想知道是否可以做到这一点 当 XML 响应有不正确的值需要映射到枚举时 这将是一个很大的帮助 我具体处理的情况是当期望值有尾随空格而枚举期望它没有尾随空格时 XML
  • Android 编程中 b/w app:theme 和 android:theme 有什么区别

    我看到有人写这个 app theme style xyz 另一方面 有些人写 android theme style xyz 这2个代码有什么区别 app and android实际上是命名空间 你可以用同样的方式来思考它们packages
  • IdHTTP 如何发送原始正文

    我如何使用IdHTTP发送消息为PostMan下面的dos 我的第一次尝试如下 function TIdFoo SendIM const AID AMessage string Boolean const URL https URL com