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

Author Topic: HTML setHost  (Read 7040 times)

0 Members and 1 Guest are viewing this topic.

Hey

  • Newbie
  • *
  • Posts: 5
    • View Profile
HTML setHost
« on: July 17, 2018, 07:36:53 am »
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;
}
« Last Edit: July 17, 2018, 07:39:08 am by Hey »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: HTML setHost
« Reply #1 on: July 17, 2018, 09:34:08 am »
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?
Laurent Gomila - SFML developer

Hey

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: HTML setHost
« Reply #2 on: July 17, 2018, 04:52:09 pm »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: HTML setHost
« Reply #3 on: July 17, 2018, 09:30:49 pm »
The next obvious step would be to Google "HTTP status code 301" and understand what it does and what it means and then you can handle it. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hey

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: HTML setHost
« Reply #4 on: July 18, 2018, 05:57:31 am »
The next obvious step would be to Google "HTTP status code 301" and understand what it does and what it means and then you can handle it. ;)

Alright I got everything working lol, is there way to get a specific part of the html?

I tried messing around with setField and setBody. They dont seem to work. I know that getBody returns one big string, and one way I know is to read the string and get data from it. Is there a better way or should i just continue with my way?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: HTML setHost
« Reply #5 on: July 18, 2018, 07:59:49 am »
getBody() returns the full HTML body, how to interpret or use it is up to you. And since you don't tell us what you want to get from that body, there's very little we can do for you ;)
Laurent Gomila - SFML developer

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Re: HTML setHost
« Reply #6 on: August 19, 2018, 04:33:42 am »
The next obvious step would be to Google "HTTP status code 301" and understand what it does and what it means and then you can handle it. ;)

Alright I got everything working lol, is there way to get a specific part of the html?

I tried messing around with setField and setBody. They dont seem to work. I know that getBody returns one big string, and one way I know is to read the string and get data from it. Is there a better way or should i just continue with my way?

The optimal method of retrieving such data is by sending a request to a REST API and then using a JSON parser to access the data.

For example:
Quote
https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22

Returns:
Quote
{"coord":{"lon":139.01,"lat":35.02},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":285.514,"pressure":1013.75,"humidity":100,"temp_min":285.514,"temp_max":285.514,"sea_level":1023.22,"grnd_level":1013.75},"wind":{"speed":5.52,"deg":311},"clouds":{"all":0},"dt":1485792967,"sys":{"message":0.0025,"country":"JP","sunrise":1485726240,"sunset":1485763863},"id":1907296,"name":"Tawarano","cod":200}

API Documentation:
https://openweathermap.org/current

JSON Parser (header library):
https://github.com/nlohmann/json
« Last Edit: August 19, 2018, 04:35:25 am by Turbine »

 

anything