Question : Parsing a HttpResponse (C++)

Experts,

I'm currently getting a Http response as a binary string and writing it to a log file (below). Instead I'd like to parse it as a string. E.g. if the response contains an XML tag <response>200</response> I'd like to parse that out.

How would I go about doing this?

thanks!

HttpWebResponse^ httpResponse = nullptr;
BinaryReader^ httpResponseStream = nullptr;

                  httpResponse = (HttpWebResponse^)httpRequest->GetResponse();
                  httpResponseStream = gcnew BinaryReader(httpResponse->GetResponseStream(), Encoding::UTF8);
                  array<Byte>^ readData;
                  // Write Response To File
            while (true)
            {
                readData = httpResponseStream->ReadBytes(4096);
                if (readData->Length == 0)
                    break;
                logFile->Write(readData, 0, readData->Length);
            }

Answer : Parsing a HttpResponse (C++)

You can use StreamReader to get the response as text....

httpResponse = (HttpWebResponse^)httpRequest->GetResponse();

StreamReader^ reader = gcnew StreamReader (httpResponse->GetResponseStream());

string^ textData = reader->ReadToEnd();

// To parse the xml data (to get the value 200 in <response>200</response>)

XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( textData );

XmlNode^ root = doc->DocumentElement;
XmlNode^ node = root->SelectSingleNode( "response" );

String^ responseValue = node->InnerText;

Hope this helps.
Random Solutions  
 
programming4us programming4us