【问题标题】:Using Poco::Net::StreamSocket socket to download file from internet?使用 Poco::Net::StreamSocket 套接字从互联网下载文件?
【发布时间】:2012-09-24 03:10:39
【问题描述】:

我想使用 Poco::Net::StreamSocket 套接字从互联网下载文件。 我需要一个源模板。 谁来帮帮我!

【问题讨论】:

  • 欢迎来到stackoverflow。请花几分钟阅读what have you tried 和SSCCE,以及FAQ。它将帮助您发布更好的问题。
  • 您想像浏览器那样通过 HTTP 下载文件吗?您可以为此使用 Poco 的 HTTPClientSession,请参阅文档here

标签: c++ poco-libraries


【解决方案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的示例代码。

【讨论】:

    【解决方案2】:

    查看Poco network slides 的第 11 页,它应该有助于您入门。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多