Quantcast
Channel: WE MOVED to github.com/microsoft/cpprestsdk. This site is not monitored!
Viewing all articles
Browse latest Browse all 4845

New Post: REST equivalent for response.Content.ReadAsByteArrayAsync().Result

$
0
0
Hello,

I am trying to translate some C# code to C++ and these lines are not working in any way I tried:
var response = ApiClient.SendAsync(request).Result;
/* Read the response */
var content = response.Content.ReadAsByteArrayAsync().Result;
/* Save response to disk */
using(FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
      {
           fs.Write(content, 0, content.Length);
       }
I found many examples here, here, here but nothing can fit my problem

One of my non working version is this:
Concurrency::streams::istream bodyStream = response.body();
ReadStreamWriteFile(bodyStream);
The ReadStreamWriteFile method is this:
void ReadStreamWriteFile (Concurrency::streams::istream inStream)
{
container_buffer<std::string> inStringBuffer;
std::string bufferToWrite = "";
while(!inStream.is_eof())
{
inStream.read_line(inStringBuffer);
const std::string line = inStringBuffer.collection();
bufferToWrite = line;
}
std::ofstream fileStreamOut;
fileStreamOut.open ("response.txt", ios::binary );
fileStreamOut.write(bufferToWrite.c_str(), bufferToWrite.length());
fileStreamOut.close();
}
The response.txt is like this:
line1line2line3line4line5.....EOF 
and should be like this:
line1
line2
line3
line4
line5
......
EOF
Another non working version is this:
Concurrency::streams::istream bodyStream = response.body();
std::string bufferToWrite = "";
while (!bodyStream.is_eof())
{           
    std::cout << c;
    try{
        std::ofstream fileStreamOut;
        fileStreamOut.open ("response.txt", ios::binary );
        c = bodyStream.extract<unsigned char>().get();
        bufferToWrite +=c;
        fileStreamOut.write(bufferToWrite .c_str(), bufferToWrite .length());
        fileStreamOut.close();
    }
    catch(std::exception &e)
    {
        std::cout << "exception !!!!!!!" << e.what() << endl;
    }

}
The response.txt is the same.

Can someone please tell me how to use read_line or read for an istream?
Or how to detect the '\n' using
bodyStream.extract<char>.get() ?
Thanks,
Adrian

Viewing all articles
Browse latest Browse all 4845

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>