unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdZLibCompressorBase,
IdCompressorZLib, Vcl.StdCtrls;
type
TForm1 = class(TForm)
IdCompressorZLib1: TIdCompressorZLib;
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IdHTTP;
function post_http(const url: string; t: tstringlist): string;
var memstr,outstr: TStringStream;
idhttp1: Tidhttp;
j: integer;
label pp;
begin
j:= 0;
//screen.Cursor:= crhourglass;
memstr:= TStringStream.Create('',TEncoding.UTF8);
pp:
idhttp1:= Tidhttp.Create(nil);
IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol]; //必须有这行才使设置协议版本生效
IdHTTP1.ProtocolVersion := pv1_1;
idhttp1.HandleRedirects:=True;
IdHTTP1.Request.AcceptEncoding :='gzip';
idhttp1.ReadTimeout:= 30000;
idhttp1.ConnectTimeout := 10000;
IdHttp1.Request.Referer := 'http://www.cha128.com/';
//idhttp1.AllowCookies:= false;
try
if t=nil then
IdHTTP1.get(url,memstr)
else
IdHTTP1.Post(url,t,memstr);
except
if j=0 then
j:= 1
else begin
result:= 'neterror';
memstr.Free;
idhttp1.Free;
//screen.Cursor:= crdefault;
exit;
end;
end;
if j=1 then
begin
j:= 2;
idhttp1.Free;
sleep(2000);
goto pp; //网络错误时重试一次
end;
if IdHTTP1.Response.ContentEncoding = 'gzip' then
begin
outstr:= TStringStream.Create('',TEncoding.UTF8);
if memstr.Size=0 then
result:='返回数据为空'
else begin
memstr.Position:= 0;
form3.IdCompressorZLib1.DecompressGZipStream(memstr, outstr);
outstr.Position := 0;
result:= outstr.DataString;
//memo1.Lines.Add('压缩前'+inttostr(outstr.Size));
//memo1.Lines.Add('压缩后'+inttostr(memstr.Size));
end;
outstr.Free;
end else begin
result:= memstr.DataString;
end;
idhttp1.Free;
//screen.Cursor:= crdefault;
memstr.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tt:tstringlist;
begin
tt:=tstringlist.Create;
tt.Add('text='+memo1.Text);
memo2.Text:=post_http('http://www.xxx.com/api/geturl/getBaseDomain.php?text=',tt);
end;
end.