c++ - boost :asio. 从服务器下载镜像文件

标签 c++ boost-asio

我正在尝试从 HTTP 服务器的响应中保存图像文件 (gif),但该文件未正确保存。

我正在使用 boost 网页中的同步代码示例,我得到了一个文件,但它与您在网络浏览器中看到的文件不同。在此处查看原始文件(WMS 服务器):http://demo.lizardtech.com/lizardtech/iserv/ows?SERVICE=WMS&REQUEST=GetMap&LAYERS=LACounty,&STYLES=&BBOX=314980.5,3624089.5,443200.5,3861209.5&SRS=EPSG:26911&FORMAT=image/gif&HEIGHT=300&WIDTH=600

我尝试了不同的方法,但没有人在工作。我得到一个文件,但不是同一个文件。你知道哪里/出了什么问题吗?谢谢。

#include <fstream>
#include <string>
#include <boost/asio.hpp>

using namespace std;
using namespace boost::asio;
using boost::asio::ip::tcp;

void GetFile(const std::string& serverName,
                            const std::string& getCommand,
                            std::ofstream& outFile)
{
   boost::asio::io_service io_service;

   // Get a list of endpoints corresponding to the server name.
   tcp::resolver resolver(io_service);
   tcp::resolver::query query(serverName, "http");
   tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
   tcp::resolver::iterator end;

   // Try each endpoint until we successfully establish a connection.
   tcp::socket socket(io_service);
   boost::system::error_code error = boost::asio::error::host_not_found;
   while (error && endpoint_iterator != end)
   {
     socket.close();
     socket.connect(*endpoint_iterator++, error);
   }

   boost::asio::streambuf request;
   std::ostream request_stream(&request);

   request_stream << "GET " << getCommand << " HTTP/1.0\r\n";
   request_stream << "Host: " << serverName << "\r\n";
   request_stream << "Accept: */*\r\n";
   request_stream << "Connection: close\r\n\r\n";

   // Send the request.
   boost::asio::write(socket, request);

   // Read the response status line.
   boost::asio::streambuf response;
   boost::asio::read_until(socket, response, "\r\n");

   // Check that response is OK.
   std::istream response_stream(&response);
   std::string http_version;
   response_stream >> http_version;
   unsigned int status_code;
   response_stream >> status_code;
   std::string status_message;
   std::getline(response_stream, status_message);


   // Read the response headers, which are terminated by a blank line.
   boost::asio::read_until(socket, response, "\r\n\r\n");

   // Process the response headers.
   std::string header;
   while (std::getline(response_stream, header) && header != "\r")
   {
   }

   // Write whatever content we already have to output.
   if (response.size() > 0)
   {
      outFile << &response;
   }
   // Read until EOF, writing data to output as we go.
   while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
   {
      outFile << &response;                
   }
}


int main(int argc, char* argv[])
{
   string serverName = "demo.lizardtech.com";

   string getCommand = "/lizardtech/iserv/ows?SERVICE=WMS&REQUEST=GetMap&";
   getCommand += "LAYERS=LACounty,&STYLES=&";            
   getCommand += "BBOX=314980.5,3624089.5,443200.5,3861209.5&";
   getCommand += "SRS=EPSG:26911&FORMAT=image/gif&HEIGHT=300&WIDTH=600";

   std::ofstream outFile("image.gif", std::ofstream::out, std::ofstream::binary);

   GetFile(serverName, getCommand, outFile);

   outFile.close();

   return 0;
}

最佳答案

看起来输出文件流配置错误。尝试更换

std::ofstream outFile("image.gif", std::ofstream::out, std::ofstream::binary);

 std::ofstream outFile("image.gif", std::ofstream::out | std::ofstream::binary);

注意第二个逗号替换为 bitwase 或 ( | )

我还建议查看 URDL由 boost::asio 的创建者编写。使此类任务变得容易得多。

关于c++ - boost :asio. 从服务器下载镜像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422094/

相关文章:

C++ lambda,没有看到函数和参数?

c++ - 在套接字关闭之前不发送数据

c++ - 通过 boost::asio 发送原始数据

c++ - 链接 boost 库

c++ - 访问 typedef 映射结构中的成员?

c++ - 如何在 C++ 中使用比较运算符比较日期和时间

c++ - boost asio,如何取消异步操作

c++ - 将在c++中输出多维数组内容的for循环的结构是什么

c++ - 当鼠标悬停在边框上时,如何完全禁用调整窗口大小,包括调整大小图标?

c++ - boost::asio::ssl 多线程应用程序访问冲突