保持 HTTP- 连接在 pascal 中?

Keep HTTP- Connection in pascal?

我想让一个程序自动访问这个网站http://ringzer0team.com/ 我需要的一切(用户、通行证、csfr)我都有,但是如何保持 HTTP - 连接在 pascal 中?我使用 http://posttestserver.com/ 来测试方法 post。 这是我的代码。当我检查 login.html 文件时。它说 HTTP_CONNECTION = 关闭 所以我想知道如何保持它。

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,StdCtrls, httpsend,synacode; 



procedure TForm1.Button2Click(Sender: TObject);
  var
    URL: string;
    Params: string;
    Response: TMemoryStream;
    i:longint;
    S:ansistring;
    cs:string;
  begin
    httpClient:= THTTPSend.Create;
    if httpClient.HTTPMethod('GET', 'http://ringzer0team.com/login') then
    httpClient.Document.SaveToFile('1.txt');
    httpClient.Free;
    system.assign(fi,'1.txt');
    reset(fi);
    for i:=1 to 62 do readln(fi,S);
    cs:=copy(S,21,32);
    system.close(fi);
    Response := TMemoryStream.Create;
    try
        URL := 'http://posttestserver.com/post.php?dump&html';
        Params := 'username=' + EncodeURLElement('user') + '&' + 'password=' + EncodeURLElement('pass') + '&' + 'csrf=' + EncodeURLElement(cs);
        system.assign(fi,'text');
        if HttpPostURL(URL, Params, Response) then Response.SaveToFile('login.html');
    finally
    Response.Free;
    end;
   end;

我可以建议你使用更简单的方法。看: 首先,复制我的 "parser"-函数:http://pastebin.com/u7C2xM67

其次,您不必将服务器答案作为文件保存到您的电脑上,因为这样做是可以的: 从 http://pastebin.com/dxHLYERq 复制我的函数 "GetHTTPStr" 并将其用作

someStringVariable := GetHttpStr(httpClient)

使用函数 "parser" 你也可以获得 csrf-token。就像

//我使用 "res" 变量作为 "response" 字符串,只需创建 res:string 变量并使用 res:=GetHttpStr(httpClient)

cs := parser(res, "var _", ";");

//现在 cs 看起来像 6a5b8e94 = 'token_here' 并从 self

获取令牌
cs := parser(cs, #39, #39); //#39 is a code of symbol '

所以我们从“到”进行解析并获得我们需要的令牌

要post数据到服务器可以使用httpClient.HTTPMethod函数。 首先你需要将你的 post 数据放入请求 body,你可以用

var params:string; //your post data, it must have view like param1=value1&param2=value2 
httpClient.Document.Write(Pointer(params)^, Length(params)); //it adds your data into request body

现在您可以使用 httpClient.HTTPMethod('POST','ringzer.com/login');

发送请求

不要忘记在每次发送 POST-requests 时更改请求 content-type header。可以用 httpClient.mimeType := 'application/x-www-form-urlencoded'; 来完成 如果你做了 POST-request 并且想在将来使用 httpsend-object 来制作 get-request 你应该把它改回 text/html

回答您的问题,如果您想提出 non-closed 请求,您可以在 httpsend-object 中添加 header:

httpClient.Headers.Add('Connection: keep-alive'); //Headers has a tstringlist type

不要忘记在每次使用 httpClient.clear;

请求后清除 httpsend-object