how to make http get request using TclientSocket?

800 Views Asked by At

I tried the following HTTP GET request

function CreateHTTPRequest(Site: String): String;
var
    Request: String;
begin
    Randomize;
    Request := 'GET ' + Site + ' HTTP/1.1' + #13#10;
    Request := Request + 'Host: ' + Site + #13#10;
    Request := Request + 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' + #13#10;
    Request := Request + 'Accept-Language: en-us,en' + #13#10;
    Request := Request + 'User-Agent: ' + UserAgent + #13#10;
    Request := Request + 'Referer: ' + Referer + #13#10;
    Request := Request + 'Connection: close' + #13#10#13#10;
    Result := Request;
end;

var
    httpSocket: TClientSocket;
    Site: String;
    Target : string;
begin
    httpSocket := TClientSocket.Create(nil);
    httpSocket.Host := 'www.google.com';
    httpSocket.Port := 80;
    httpSocket.ClientType := ctBlocking;
    httpSocket.Active := True;
    if httpSocket.Socket.Connected=True then
    begin
        memo1.Lines.Add('requested');
        Site := 'http://' + 'google.com';
        httpSocket.Socket.SendText(CreateHTTPRequest(Site));
        memo1.Lines.Add(httpSocket.Socket.ReceiveText);
        httpSocket.Active := False;
    end;

    httpSocket.Free;
end;

I don't get any response from this. What did I do wrong? I cannot do any more HTTPS requests with TclientSocket. Is it dead already?

0

There are 0 best solutions below