Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Download File Http  (Read 4055 times)

0 Members and 1 Guest are viewing this topic.

kakami okatsu

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Download File Http
« on: January 20, 2013, 03:19:53 pm »
Ih all I'm an italian student.
In my project I want to download an image by a url. Everything is fine until I try to download an image bigger than 4KB. The problem (i think) is the max size of string.
I'll post the code. Maybe there is an error!
string DownloadHtml(string Url){  
         //Split the url (Why I have to do this??)
         unsigned EndBase = Url.find("/",7); //Jump over the "http://" and search for another "/"
         string BaseURL = Url.substr(0,EndBase);
         string Page = Url.substr(EndBase);
         // Create a new HTTP client
         sf::Http http;

         http.setHost(BaseURL);

         // Prepare a request
         sf::Http::Request request(Page);

         // Send the request
         sf::Http::Response response = http.sendRequest(request);
         // Check the status code and display the result
         sf::Http::Response::Status status = response.getStatus();
         if (status == sf::Http::Response::Ok)
         {
                 return response.getBody();
         }
         else
         {
                 std::cout << "Error " << status << std::endl;
         }
}

//Load the image from Url into sf::Texture
sf::Texture GetCopertina(string Url){
        sf::Texture Txt;
        string FileCopertina = DownloadHtml(Url);
        long int Dim = FileCopertina.length();
        Txt.loadFromMemory(FileCopertina.c_str(),Dim);
        return Txt;
}
 
It's possible to download a file with http and sfml?
« Last Edit: January 20, 2013, 03:23:21 pm by kakami okatsu »

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Download File Http
« Reply #1 on: January 23, 2013, 02:12:11 am »
I haven't tested your code, but I use this (it was originally from somebody else on this forum actually, I just adapted it. Not trying to steal any credit here:P):
void DownloadFile(const std::string& Host, const std::string& RemoteFile, const std::string& LocalFile, unsigned short Port = 0)
{
        sf::Http Http;
        sf::Http::Request Request;
        unsigned int FileSize = 0;
       
        Http.setHost(Host,Port);
        Request.setMethod(sf::Http::Request::Get);
        Request.setUri(RemoteFile);
       
        sf::Http::Response Page = Http.sendRequest(Request);//Get the data
       
        FileSize = Page.getBody().size();//Get the size that was returned
       
        std::string FileContainer = Page.getBody();
        std::ofstream File(LocalFile.c_str(), std::ios::out | std::ios::binary);
        File.write(FileContainer.c_str(), FileSize);//Write the file
        File.close();
}
 

As you can see my function saves the file to the local drive, however you can modify it to return the string rather than writing the string to the file.

kakami okatsu

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Download File Http
« Reply #2 on: January 26, 2013, 12:26:05 am »
Thank you so much! The problem was on my code.
Your code solve the error!

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Download File Http
« Reply #3 on: January 26, 2013, 02:21:56 am »
Thank you so much! The problem was on my code.
Your code solve the error!

No problem, glad I could help!

 

anything