升压:ASIO。从服务器下载图像文件

时间:2014-01-29 04:26:26

标签: c++ boost-asio

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

我正在使用boost网页中的同步代码示例,我收到了一个文件,但它与您在Web浏览器中看到的文件不同。请在此处查看原始文件(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;
}

1 个答案:

答案 0 :(得分:8)

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

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

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

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

我还建议通过boost :: asio的创建者查看URDL。使这些任务变得更加容易。