Mega Code Archive

 
Categories / Delphi / Strings
 

Getting a page from a webserver and put it in a string variable

Title: Getting a page from a webserver and put it in a string variable. Question: How do I get a page from a webserver only using TClientSocket? (and put it into a string variable) Answer: { Attach the following event-handlers to your TClientSocket. It gets the file from the server and puts it in the FText string variable. Btw, it doesn't remove the header that is also send by the webserver. Don't forget to setup your Socket object with a correct server adress. Set port to 80. And open it with "Socket.Open;". I wrote this as part of an AutoUpdate feature in one of my applications. E.J.Molendijk } Const WebPage = '/index.html'; Var FText : String; procedure TForm1.SocketWrite(Sender: TObject; Socket: TCustomWinSocket); begin Socket.SendText('GET '+Webpage+' HTTP/1.0'#10#10); end; procedure TForm1.SocketRead(Sender: TObject; Socket: TCustomWinSocket); begin FText := FText + Socket.ReceiveText end; procedure TForm1.SocketConnecting(Sender: TObject; Socket: TCustomWinSocket); begin FText := ''; end; procedure TForm1.SocketDisconnect(Sender: TObject; Socket: TCustomWinSocket); begin { --- } { HERE YOU CAN PROCESS YOUR FText !!! } { --- } end; procedure TForm1.SocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin ErrorCode:=0; { Just ignore errors } end;