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?
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.