频道分类

Delphi NetHttpClient post 函数

作者:admin 来源: 日期:2019/10/7 23:16:02 人气: 标签:

 
uses System.Net.URLClient,System.Net.HttpClient, System.Net.HttpClientComponent;

function HttpPost(Url: String; ID: Integer; Cookie: string): String;
  var
  NetHttp: TNetHTTPClient;
  TmpSS: TStringStream;
  ParmList: TStringList;
begin
  Result := '';
  NetHttp := TNetHTTPClient.Create(nil);
  ParmList := TStringList.Create;
  TmpSS := TStringStream.Create('', TEncoding.UTF8);
  try
    //NetHttp.Asynchronous := True;  异步
    NetHttp.ConnectionTimeout := 10000;
    NetHttp.ResponseTimeout := 20000;
   // NetHttp.CustomHeaders['Cookie'] := 'sscSESSID=' + Cookie;
    NetHttp.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
    ParmList.Add('op=getCurIssue');
    ParmList.Add('op=getCurIssue');
    ParmList.Add('lotteryId=' + IntToStr(ID));
    NetHttp.Post(Url, ParmList, TmpSS);
    Result := Trim(TmpSS.DataString);
  finally
    NetHttp.Free;
    ParmList.Free;
    TmpSS.Free;
  end;
end;



procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Memo1.Text:= HttpPost('http://www.baidu.com/',1,'123');
end;