Solved

Parsing a HttpResponse (C++)

Posted on 2010-08-24
3
740 Views
Last Modified: 2012-05-10
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);
            }
0
Comment
Question by:php-newbie
3 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33514656
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.
0
 

Author Closing Comment

by:php-newbie
ID: 33516975
Thanks, awesome!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now