0

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?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Raelpaul
  • 31
  • 5
  • yes, it is dead - you set the command: `'Connection: close'` - thus the server closes the connection of your socket. – Arioch 'The Jan 09 '17 at 02:19
  • http://stackoverflow.com/questions/301546/whats-the-simplest-way-to-call-http-get-url-using-delphi – Arioch 'The Jan 09 '17 at 02:20
  • http://stackoverflow.com/a/17549592/976391 – Arioch 'The Jan 09 '17 at 02:27
  • Possible duplicate of [What's the simplest way to call Http GET url using Delphi?](http://stackoverflow.com/questions/301546/whats-the-simplest-way-to-call-http-get-url-using-delphi) – David Rawson Jan 09 '17 at 02:40
  • 2
    In `CreateHTTPRequest()`, the request line needs to be `GET / HTTP/1.1` instead of `GET http://google.com HTTP/1.1`, and the `Host` header needs to be `Host: google.com` instead of `Host: http://google.com`. Also, `SendText()` is not guaranteed to send the entire string in one go, you may have to call `SendText()` multiple times, but you are ignoring its return value. And `ReceiveText` is the completely wrong way to read the server's response. Why you are using `TClientSocket` to implement HTTP manually instead of using Indy's `TIdHTTP`, or any other HTTP component/library for that matter? – Remy Lebeau Jan 09 '17 at 04:13
  • @RemyLebeau I am a beginner can you help me to migrate that code to idhttp I don't know how to do – Raelpaul Jan 09 '17 at 08:14
  • @Raelpaul `TIdHTTP` has an overloaded `Get()` method that takes a URL as input and returns a `String` as output, eg: `var http: TIdHTTP; begin http := TIdHTTP.Create; try memo1.Lines.Add('requested'); memo1.Lines.Add(http.Get('http://www.google.com')); finally http.Free; end; end;` it is slightly more complex if you need to use HTTPS, but not much (assign an SSLIOHandler component to the `TIdHTTP.IOHandler` property, such as `TIdSSLIOHandlerSocketOpenSSL`, and deploy the two OpenSSL DLLs with your app). Also see http://indyproject.org/Sockets/Blogs/ChangeLog/20141222.aspx. – Remy Lebeau Jan 09 '17 at 09:39

0 Answers0