Search code examples
freepascallazarushttpconnectionapache-synapse

Keep HTTP- Connection in pascal?


I want to make a program auto access this site http://ringzer0team.com/ Everything I need (user,pass,csfr) I have, but how to keep the HTTP - Connection in pascal? I used http://posttestserver.com/ to test the method post. Here is my code. When I check the login.html file . It says HTTP_CONNECTION = close So I wonder how to keep it on.

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;

Solution

  • I can advice you to use more easier way. Look: First, copy my "parser"-function: http://pastebin.com/u7C2xM67

    Second, you don`t have to save server answers as file to your pc, because it can be done so: Copy my function "GetHTTPStr" from http://pastebin.com/dxHLYERq and use it as

    someStringVariable := GetHttpStr(httpClient)
    

    Using function "parser" you can get csrf-token too. It would be like

    //I use "res" variable as "response" string, just make res:string variable and use res:=GetHttpStr(httpClient)

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

    //now cs looks like 6a5b8e94 = 'token_here' and getting token from self

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

    so we parsing from ' to ' and getting token we need

    To post data to server you can use httpClient.HTTPMethod function. First you need to put your post data into request body, you can do it with

    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
    

    Now you can send your request using httpClient.HTTPMethod('POST','ringzer.com/login');

    Do not forget to change request content-type header every time you send POST-requests. It can be done with httpClient.mimeType := 'application/x-www-form-urlencoded'; And if you did POST-request and want to use httpsend-object to make get-request in future you should change it back to text/html

    Answering your question, if you want to make a non-closed request you can add a header to httpsend-object:

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

    Do not forget to clear httpsend-object after each request using httpClient.clear;