2014-01-29 34 views
5

我试图从HTTP服务器的响应保存图像文件(gif),但该文件未正确保存。提升:asio。从服务器下载图像文件

我使用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; 
} 
+0

您是否试图比较正确文件的大小和内容以及您设法保存的文件?你确定你的'header!=“\ r”'test,不应该是'“\ r \ n”'? –

回答

6

它看起来像输出文件流配置错误。试着用

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

注意更换

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

与bitwase或更换第二个逗号(|)

我也建议由升压:: ASIO的创造者看着URDL。使这些任务变得更容易。