unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, CnMD5,
CnBase64,WinINet,IDURI,Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure Post(url, data, Len, Auth: string; res: TStream);
var
hInt, hConn, hreq: HINTERNET;
buffer: PChar;
dwRead, dwFlags: cardinal;
port: Word;
uri: TIdURI;
proto, host, path: string;
header:string;
begin
uri := TIdURI.Create(url);
host := uri.Host;
path := uri.Path + uri.Document + uri.Params;
proto := uri.Protocol;
uri.Free;
if UpperCase(proto) = 'HTTPS' then
begin
port := INTERNET_DEFAULT_HTTPS_PORT;
dwFlags := INTERNET_FLAG_SECURE;
end
else
begin
port := INTERNET_INVALID_PORT_NUMBER;
dwFlags := INTERNET_FLAG_RELOAD;
end;
hInt := InternetOpen('Delphi', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConn := InternetConnect(hInt, PChar(host), 8883, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hreq := HttpOpenRequest(hConn, 'POST', PChar(Path), 'HTTP/1.1', nil, nil, dwFlags, 0);
GetMem(buffer, 65536);
header:='Content-Length:'+len+
#13'Accept:application/json'+
#13'Content-Type:application/json'+
#13'Authorization:'+auth;
if HttpSendRequest(hReq, PAnsiChar(header), Length(header), PChar(data), Length(data)) then
begin
dwRead := 0;
repeat
InternetReadFile(hreq, buffer, 65536, dwRead);
if dwRead <> 0 then
res.Write(buffer^, dwRead);
until dwRead = 0;
end;
InternetCloseHandle(hreq);
InternetCloseHandle(hConn);
InternetCloseHandle(hInt);
FreeMem(buffer);
end;
procedure TForm2.Button1Click(Sender: TObject);
var
s: string;
sig: string;
b64: string;
t: string;
l,content: string;
Responses: TStringStream;
begin
t := formatdatetime('yyyyMMddHHnnss', Now);
sig := UpperCase(MD5Print(MD5String('123123123' + '234234234234' + t)));
Base64Encode('2221232321:' + t, b64);
content:=AnsiToUtf8('{"from":"13155555555","to":"18822225555"}');
l := IntToStr(Length(content));
Responses := TStringStream.Create('');
s := 'https://www.good.com:856/Callback?sig=' + sig + '';
Post(s, content,l,b64, Responses);
Memo1.text := Utf8ToAnsi(Responses.DataString);
Responses.Free;
end;
end.
- 1
- 2
- 3
- 4
- 5
- 6
前往页