使用Poco :: Net :: StreamSocket套接字从互联网下载文件?

时间:2012-09-24 03:10:39

标签: c++ poco-libraries

我想使用Poco :: Net :: StreamSocket套接字从互联网上下载文件。 我需要一个源模板。 任何人都帮助我!

2 个答案:

答案 0 :(得分:1)

此代码对我有用:

#include "Poco/URIStreamOpener.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/FTPStreamFactory.h"
#include <memory>
#include <iostream>

using Poco::URIStreamOpener;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;
using Poco::Net::HTTPStreamFactory;
using Poco::Net::FTPStreamFactory;

int main(int argc, char** argv)
{
  HTTPStreamFactory::registerFactory(); // Must register the HTTP factory to stream using HTTP
  FTPStreamFactory::registerFactory(); // Must register the FTP factory to stream using FTP

  string url = "http://somefile.mp3";
  string filePath = "C:\\somefile.mp3";

  // Create and open a file stream
  std::ofstream fileStream;
  fileStream.open(filePath, ios::out | ios::trunc | ios::binary);

  // Create the URI from the URL to the file.
  URI uri(url);

  // Open the stream and copy the data to the file. 
  std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));
  StreamCopier::copyStream(*pStr.get(), fileStream);

  fileStream.close();
}

以上很多内容来自找到here的示例代码。

答案 1 :(得分:0)

请查看Poco network slides的第11页,它可以帮助您入门。