我尝试通过TClientSocket从网站加载图像。我不会使用 TClinetSocket 和其他组件(如 INDY 或其他)来做到这一点。
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;
响应文本是
HTTP/1.1 200 OK
服务器:nginx/1.8.1
日期:2016 年 3 月 11 日星期五 16:53:29 GMT
内容类型:image/png
内容长度:1248
连接: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
缓存控制:私有,无缓存,无存储,必须重新验证, max-age=0
Pragma: no-cache
‰PNG
而且我在回复中看不到图像