使用idhttp发送AMF头

时间:2015-11-30 22:11:48

标签: delphi indy amf

是否可以将AMF标头发送到某个站点并获得响应

我搜索了很多但没有运气

这是我使用的代码,但我不知道如何编写amf标题(Post Data!)

procedure TForm1.Button1Click(Sender: TObject);
 var
 SL: TStringList;
 HTTPAgent,FlexEngine ,FlexContent,ContentType:string;
begin


 HTTPAgent   := 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 OPR/33.0.1990.115';
 FlexEngine  := 'http://somesite.com/tools/amf.php';
 ContentType := 'application/x-amf';

 IdHTTP1.Request.UserAgent := HTTPAgent;

 SL := TStringList.Create;
 SL.Add(); // how to write the header

 IdHTTP1.Request.ContentType := ContentType;

 try
 memo1.Text :=  IdHTTP1.Post(FlexEngine, SL);
 finally
  SL.Free;
end;

end;

这篇文章看起来如何

header Data from http analyzer

1 个答案:

答案 0 :(得分:2)

使用TStringList发布数据会格式化application/x-www-form-urlencoded格式的数据,这不是您希望在这种情况下发生的情况。您需要使用TStream发布您的AMF数据,以便按原样发布,例如:

unit AMF;

interface

uses
  Classes;

procedure WriteUInt8ToStream(Stream: TStream; Value: Byte);
procedure WriteUInt16ToStream(Stream: TStream; Value: Word);
procedure WriteInt32ToStream(Stream: TStream; Value: Integer);
procedure WriteUInt32ToStream(Stream: TStream; Value: LongWord);
procedure WriteStringToStream(Stream: TStream; const Value: String);

implementation

uses
  IdStack;

procedure WriteUInt8ToStream(Stream: TStream; Value: Byte);
begin
  Stream.WriteBuffer(Value, Sizeof(Value));
end;

procedure WriteUInt16ToStream(Stream: TStream; Value: Word);
begin
  Value := GStack.HostToNetwork(UInt16(Value));
  Stream.WriteBuffer(Value, Sizeof(Value));
end;

procedure WriteInt32ToStream(Stream: TStream; Value: Integer);
begin
  Value := Integer(GStack.HostToNetwork(UInt32(Value)));
  Stream.WriteBuffer(Value, Sizeof(Value));
end;

procedure WriteUInt32ToStream(Stream: TStream; Value: LongWord);
begin
  Value := GStack.HostToNetwork(UInt32(Value));
  Stream.WriteBuffer(Value, Sizeof(Value));
end;

procedure WriteStringToStream(Stream: TStream; const Value: String);
var
  U: UTF8String;
  Len: Word;
begin
  U := UTF8String(Value); // or UTF8Encode(Value) if using D2007 or earlier
  Len := Min(Length(U), $FFFF);
  WriteUInt16ToStream(Stream, Len);
  Stream.WriteBuffer(PAnsiChar(U)^, Len * SizeOf(AnsiChar));
end;

uses
  AMF;

procedure TForm1.Button1Click(Sender: TObject);
var
  MS: TMemoryStream;
  HTTPAgent, FlexEngine, FlexContent, ContentType: string;
begin
  HTTPAgent   := 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 OPR/33.0.1990.115';
  FlexEngine  := 'http://somesite.com/tools/amf.php';
  ContentType := 'application/x-amf';

  IdHTTP1.Request.UserAgent := HTTPAgent;

  MS := TMemoryStream.Create;
  try
    // write AMF data to stream as needed...
    AMF.WriteInt16ToStream(MS, 0); // version
    AMF.WriteUInt16ToStream(MS, 1); // header-count
    AMF.WriteStringToStream(MS, '...'); // header-name
    AMF.WriteUInt8ToStream(MS, 1); // must-understand
    AMF.WriteUInt32ToStream(MS, ...); // header-length
    // write header data as needed ...
    AMF.WriteUInt16ToStream(MS, 1); // message-count
    AMF.WriteStringToStream(MS, '...'); // target-uri
    AMF.WriteStringToStream(MS, '/1'); // response-uri
    AMF.WriteUInt32ToStream(MS, ...); // message-length
    // write message data as needed ...

    MS.Position := 0;

    IdHTTP1.Request.ContentType := ContentType;

    Memo1.Text := IdHTTP1.Post(FlexEngine, MS);
  finally
    MS.Free;
  end;
end;

我将根据AMF0AMF3规范,将其作为练习,让您了解如何编写AMF标头和消息数据。