title: boost:asio. Download image file from server
body: <p>I'm trying to save a image file (gif) from the response of a HTTP server, but the file is not saved correctly.</p>
  
  <p>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): <a href="http://demo.lizardtech.com/lizardtech/iserv/ows?SERVICE=WMS&amp;REQUEST=GetMap&amp;LAYERS=LACounty,&amp;STYLES=&amp;BBOX=314980.5,3624089.5,443200.5,3861209.5&amp;SRS=EPSG:26911&amp;FORMAT=image/gif&amp;HEIGHT=300&amp;WIDTH=600" rel="noreferrer">http://demo.lizardtech.com/lizardtech/iserv/ows?SERVICE=WMS&amp;REQUEST=GetMap&amp;LAYERS=LACounty,&amp;STYLES=&amp;BBOX=314980.5,3624089.5,443200.5,3861209.5&amp;SRS=EPSG:26911&amp;FORMAT=image/gif&amp;HEIGHT=300&amp;WIDTH=600</a></p>
  
  <p>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.</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using namespace std;
  using namespace boost::asio;
  using boost::asio::ip::tcp;
  
  void GetFile(const std::string&amp; serverName,
                              const std::string&amp; getCommand,
                              std::ofstream&amp; 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 &amp;&amp; endpoint_iterator != end)
     {
       socket.close();
       socket.connect(*endpoint_iterator++, error);
     }
  
     boost::asio::streambuf request;
     std::ostream request_stream(&amp;request);
  
     request_stream &lt;&lt; "GET " &lt;&lt; getCommand &lt;&lt; " HTTP/1.0\r\n";
     request_stream &lt;&lt; "Host: " &lt;&lt; serverName &lt;&lt; "\r\n";
     request_stream &lt;&lt; "Accept: */*\r\n";
     request_stream &lt;&lt; "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(&amp;response);
     std::string http_version;
     response_stream &gt;&gt; http_version;
     unsigned int status_code;
     response_stream &gt;&gt; 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) &amp;&amp; header != "\r")
     {
     }
  
     // Write whatever content we already have to output.
     if (response.size() &gt; 0)
     {
        outFile &lt;&lt; &amp;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 &lt;&lt; &amp;response;                
     }
  }
  
  
  int main(int argc, char* argv[])
  {
     string serverName = "demo.lizardtech.com";
  
     string getCommand = "/lizardtech/iserv/ows?SERVICE=WMS&amp;REQUEST=GetMap&amp;";
     getCommand += "LAYERS=LACounty,&amp;STYLES=&amp;";            
     getCommand += "BBOX=314980.5,3624089.5,443200.5,3861209.5&amp;";
     getCommand += "SRS=EPSG:26911&amp;FORMAT=image/gif&amp;HEIGHT=300&amp;WIDTH=600";
  
     std::ofstream outFile("image.gif", std::ofstream::out, std::ofstream::binary);
  
     GetFile(serverName, getCommand, outFile);
  
     outFile.close();
  
     return 0;
  }
  </code></pre>