Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost:asio. Download image file from server

Tags:

c++

boost-asio

I'm trying to save a image file (gif) from the response of a HTTP server, but the file is not saved correctly.

I'm using the syncronous code example from the boost webpage, and I get a file, but is not the same file that you can see in a web browser. See original file here (WMS server): 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

I tried different things, but no one is working. I get a file, but is not the same file. Do you know where/what is the problem? Thanks.

#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;
}
like image 250
user1166149 Avatar asked Jan 29 '14 04:01

user1166149


1 Answers

It looks like output file stream is misconfigured. Try replacing

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

with

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

Note the second comma replaced with bitwase or ( | )

I'd also suggest looking at URDL by creator of boost::asio. Makes such tasks a lot easier.

like image 99
Anton Avatar answered Sep 20 '22 13:09

Anton