views:

512

answers:

2

How to download webpage into string without saving this page to disk in C++?

URLDownloadToFile MSDN function only saving page into disk.

+1  A: 

Did you look up the other URLXXX functions mentioned thereof? How about: URLOpenBlockingStream?

//implement filestream that derives from IStream
class StringStream : public IStream
{
  StringStream(std::wstring buf) 
    {
        _refcount = 1;
        _mBuf = buf;
    }

    ~ StringStream ()
    {

    }
   // implement IUknown, IStream interfaces
   private:
    std::wstring _mBuf;
    long _refcount;
 };

See the default file based IStream implementation here.

dirkgently
But how to read that IStream to string? I got IStream* pStream;string text;URLOpenBlockingStream(NULL, "http://www.mysexyserver.com/text.txt", text = istream2String(pStream);cout << text << endl;This not works
Kate26
You need to implement your own IStream (it's just an interface) over a string.
dirkgently
A: 

But how to read that IStream to string? I got

IStream* pStream;
string text;

URLOpenBlockingStream(NULL, "http://www.mysexyserver.com/text.txt", &pStream, 0, NULL);
text =pStream;
cout << text << endl;

This not works

Kate26
Of course not. You can't assign an IStream pointer to a std::string object. You need to pre-allocate the string and then use the IStream::Read() method to read the stream's data into the string's memory, for example:STATSTG stat;pStream->Stat(text.resize(stat.cbSize.QuadPart);pStream->Read(
Remy Lebeau - TeamB
Alternatively, use WinInet's InternetConnect(), HttpOpenRequest(), InternetReadFile() functions, instead of using URLDownToFile() at all.
Remy Lebeau - TeamB