Hello, I was just wondering why i get response error on some webpages and others not. My project is focused on weather, so I want to gather data from the website and read from it. For example, if i set the host to "http://www.google.com/" the program outputs the entire HTML of google.com. But I want it to output the html from the website below...
https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22&mode=html
How do i set the host to that website so it outputs the HTML from the new host?
Here is my code.
#include <SFML/Graphics.hpp>
#include <SFML\Network.hpp>
#include <iostream>
using namespace std;
int main() {
sf::Http http;
sf::Http::Request request;
sf::Http::Response response;
sf::RenderWindow window(sf::VideoMode(10, 10), "Weather");
request.setMethod(sf::Http::Request::Get);
http.setHost("http://www.google.com/");
response = http.sendRequest(request);
cout << "status: " << response.getStatus() << endl;
if (response.getStatus() == sf::Http::Response::Ok) {
cout << response.getBody() << endl;
}
else {
cout << "request failed" << endl;
}
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}
The tutorial and API documentation are quite explicit about how to build a request, especially what goes in the HTTP host and what goes into the request. Have you read it first? If so, can you show us what you tried, and tell us what kind of error you get?
Yea, i first tried to set the host to the full link like this.
http.setHost("https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22&mode=html");
I get the error 1001, which is ConnectionFailed error.
Then I figured out that anything after .org / should be in the uri. So i put that in setUri function, and here is the code.
request.setUri("/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22&mode=html");
http.setHost("http://samples.openweathermap.org/");
After that I get the error 301, which states its MovedPermanently.
So i dont know what that means.