This question is in direct relation to:
Getting HTML from response stream using Indy's IDTCPClient
I just need a version of get function to retrieve XML
function TMyConnector.GETXML(aRawHeader: String): String;
Using a idHTTP component, I could do the following
HTTP.Request.ContentType := 'text/xml';
The header of the file I am after looks like this:
<?xml version="1.0" encoding="utf-8" ?>
How can I do this using the IDTCPClient component?
The TIdHTTP.Request.ContentType
DOES NOT ask the server to send XML vs HTML. It tells the server that YOU are sending XML/HTML to the server. Very different thing.
To ask a server to send XML/HTML to you, that really depends on the server, not the client. You have a few choices, depending on the server's requirements:
Request a specific URL that only sends XML. Request a specific URL that only sends HTML. Etc.
Request a URL that invokes a server-side script that allows you to pass in an input parameter, usually (but not always) in the URL, to specify the desired output format.
Include an Accept
header in the request that specifies particular formats you are willing to receive for a given URL. The server has to be capable of representing the requested data in multiple formats.
You have a fundamental misunderstanding of how HTTP and TIdHTTP
actually work. I strongly suggest you use Wireshark and Fiddler to study how real web browsers send HTTP requests, and then learn how to replicate that in code. Also study RFC 2616. HTTP is not trivial to implement correctly manually, so by refusing to use a pre-made HTTP library, you are opening a bigger can of worms then you are probably ready for yet.
hmmmm, turns out it was easier than thought. I simply looked at the GenerateJSON method and said ok, How can I use this method for XML.
I then googled MempryStream to String and found this function
function StreamToString(aStream: TStream): string;
var
SS: TStringStream;
begin
if aStream <> nil then
begin
SS := TStringStream.Create('');
try
SS.CopyFrom(aStream, 0); // No need to position at 0 nor provide size
Result := SS.DataString;
finally
SS.Free;
end;
end else
begin
Result := '';
end;
end;
Procedure TLtLiveConnector.GenerateXML;
begin
if ResponseStream <> nil then
Begin
ResponseXML_V := StreamToString(ResponseStream);
End;
end;