Solved

Parsing a HttpResponse (C++)

Posted on 2010-08-24
3
757 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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