1

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Seems to be working; parse (or throw away) the HTTP response headers. What's your question? – Roger Lipscombe Mar 11 '16 at 17:15
  • @Roger Lipscombe I not see in response the image . How to save it? – user3274144 Mar 11 '16 at 17:21
  • Image is clearly there. – Free Consulting Mar 11 '16 at 21:23
  • 2
    HTTP is not trivial to implement by hand, it has a lot of parsing requirements and nuances. `TClientSocket` is not suitable for this task, unless you implement EVERYTHING by hand (and what you have shown is not even close to being adequate for the task). Why don't you want to use a ready-made HTTP component for this? Indy has a `TIdHTTP` component with a `Get()` method that would make this trivial, but there are other options available. If you don't want to install an HTTP component, there are plenty of HTTP-capable libraries readily available, such as WinInet/WinHTTP and libcurl. – Remy Lebeau Mar 12 '16 at 00:23

1 Answers1

1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
mjn
  • 36,362
  • 28
  • 176
  • 378