load image from web site throught TClientSocket

125 Views Asked by At

I try to load image from web site throught TClientSocket. I wont to do that with TClinetSocket and no other components like INDY or other.

  SckServer.Close;
  SckServer.port:=80;
  SckServer.Address:='127.0.0.1';
  SckServer.Open;

  procedure TForm1.Button3Click(Sender: TObject);
  begin
    img_url:='/img/logo.png';

    SckServer.Socket.SendText(
      'GET '+img_url+' HTTP/1.1' + #13#10 +
      'Host: localhost.com' + #13#10 +
      'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204' + #13#10 +
      'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1' + #13#10 +
      'Accept-Language: en-us, en;q=0.50' + #13#10 +
      'Accept-Encoding: gzip, deflate, compress;q=0.9' + #13#10 +
      'Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66' + #13#10 +
      'Keep-Alive: 300' + #13#10 +
      'Connection: keep-alive' + #13#10 +
      'Cache-Control: max-age=0' +#13#10 +
      'Referer: http://localhost.com'+ #13#10 +
      #13#10
    );      
  end;

  procedure TForm1.SckServerRead(Sender: TObject; Socket: TCustomWinSocket);
  begin
    form1.caption:='Ready';
    s:= s+SckServer.Socket.ReceiveText;

    s:=StringReplace(s, #10, '', [rfReplaceAll]);
    s:=StringReplace(s, #13, '', [rfReplaceAll]);
    s:=StringReplace(s, '¶', '', [rfReplaceAll]);

    memo1.Lines.Add(s);
  end;

Response text is

HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Fri, 11 Mar 2016 16:53:29 GMT
Content-Type: image/png
Content-Length: 1248
Connection: keep-alive
P3P: CP="NOI DEVa TAIa OUR BUS UNI STA"
Last-Modified: Fri, 11 Mar 2016 16:53:29 GMT
Expires: Fri, 11 Mar 2016 16:53:29 GMT
Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0
Pragma: no-cache

‰PNG

And I not see the image in the response

1

There are 1 best solutions below

0
On

Images are binary data, a TMemo is not capable of displaying images.

Your client code needs to read the HTTP headers first, then parse them to discover the byte size and transfer format, and then read the binary payload of the HTTP response body (in this case, 1248 bytes as indicated by the Content-Length header) into a byte array or stream, and then you can use that to create the image using TPngImage or similar class.