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

Author Topic: SFML help with web request  (Read 5265 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
SFML help with web request
« on: January 12, 2017, 07:37:50 am »
I'm currently running an application on a computer that is running the server. I wish to connect to the server and run a php script that saves data. However, I'm getting an error message informing me the connection to the server failed
Code: [Select]
1001 . Here is the code I used (Code is running in game loop):


        sf::Http::Request request("Score.php", sf::Http::Request::Post);

        ostringstream stream;
        stream << "Score=" << player.GetMoney();
        request.setBody(stream.str());

        sf::Http http("http://192.168.1.152:80/Web/Practice/Project/LeaderBoards/Score.php", 80);
        sf::Http::Response response = http.sendRequest(request);

        if(response.getStatus() == sf::Http::Response::Ok)
        {
                std::cout << "Server Response: " << response.getBody() << std::endl;
        }
        else
                std::cout << "request failed: " << response.getStatus() << std::endl;
 

I'm not sure which url I should be using if its on my own computer

sf::Http http("http://localhost/Web/Practice/Project/LeaderBoards/Score.php", 80);
 

or

sf::Http http("http://10.0.2.2:80/Web/Practice/Project/LeaderBoards/Score.php", 80);
 

What could be the problem?
« Last Edit: January 12, 2017, 10:00:29 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML help with web request
« Reply #1 on: January 12, 2017, 07:49:50 am »
Host must just be "http://192.168.1.152:80", the rest should be part of the request URI.
Laurent Gomila - SFML developer

Student555

  • Guest
Re: SFML help with web request
« Reply #2 on: January 12, 2017, 08:14:58 am »
Well, I've used "http://192.168.1.152:80" as my host.

However, I'm still getting a failed connection.

When you said the "rest should be part of the request URI", did you mean something like this:
sf::Http::Request request("/Web/Practice/Project/LeaderBoards/Score.php", sf::Http::Request::Post);

        ostringstream stream;
        stream << "Score=" << player.GetMoney();
        request.setBody(stream.str());

        sf::Http http("http://192.168.1.152:80", 80);
        sf::Http::Response response = http.sendRequest(request);

        if(response.getStatus() == sf::Http::Response::Ok)
        {
                std::cout << "Server Response: " << response.getBody() << std::endl;
        }
        else
                std::cout << "request failed: " << response.getStatus() << std::endl;

 

The above code still does not work. 1001 error
« Last Edit: January 12, 2017, 10:00:13 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML help with web request
« Reply #3 on: January 12, 2017, 08:36:47 am »
Quote
sf::Http http("http://192.168.1.152:80", 80);
If the port is specified as a separate argument, it should obviously not be duplicated into the address. And since 80 is the default, you could just write
sf::Http http("http://192.168.1.152");
Laurent Gomila - SFML developer

Student555

  • Guest
Re: SFML help with web request
« Reply #4 on: January 12, 2017, 09:45:15 am »
I have tried with
sf::Http::Request request("/Web/Practice/Project/LeaderBoards/Score.php", sf::Http::Request::Post);

        ostringstream stream;
        stream << "Score=" << player.GetMoney();
        request.setBody(stream.str());

        sf::Http http("http://192.168.1.152");
        sf::Http::Response response = http.sendRequest(request);

        if(response.getStatus() == sf::Http::Response::Ok)
        {
                std::cout << "Server Response: " << response.getBody() << std::endl;
        }
        else
                std::cout << "request failed: " << response.getStatus() << std::endl;

 

with no success.
« Last Edit: January 12, 2017, 10:00:07 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML help with web request
« Reply #5 on: January 12, 2017, 10:03:28 am »
Status 1001 means that SFML couldn't open a TCP socket on host at specified port. Are you sure that your server is working perfectly?
Laurent Gomila - SFML developer

Student555

  • Guest
Re: SFML help with web request
« Reply #6 on: January 12, 2017, 10:53:34 am »
Well, I'm trying to connect to EasyPHP. According to the main console, its running fine.

Student555

  • Guest
Re: SFML help with web request
« Reply #7 on: January 19, 2017, 04:48:54 am »
Tried using another computer with the same source code (Different IP address). It still failed with the same error
Code: [Select]
Request Failed: 1001

I am using EasyPHP as my server and both computers were listening to port 80. Both computers look like they are fine. Server panel is signaling to the server that everything is running smoothly.
When making web requests with SFML, do I have to make a

Code: [Select]
sf::TcpSocket
in the application in order for the application to connect with the server?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
SFML help with web request
« Reply #8 on: January 19, 2017, 07:46:54 am »
Try removing the first slash in the request, so Web/ instead of /Web/.

Next open your web browser and try to navigate to your local server, does it find it?
You can also use netstat to find out if your server is really listening on port 80.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: SFML help with web request
« Reply #9 on: January 19, 2017, 08:52:41 am »
Tried that
Code: [Select]
sf::Http::Request request("Web/Practice/Project/LeaderBoards/Score.php", sf::Http::Request::Post);

    ostringstream stream;
    stream << "Score=" << "Hello";
    request.setBody(stream.str());

    sf::Http http("http://192.168.1.152");
    sf::Http::Response response = http.sendRequest(request);

    if(response.getStatus() == sf::Http::Response::Ok)
    {
        std::cout << "Server Response: " << response.getBody() << std::endl;
    }
    else
        std::cout << "request failed: " << response.getStatus() << std::endl;


Server panel is below

I even tried
Code: [Select]
sf::Http::Request request("C:/Web/Practice/Project/LeaderBoards/Score.php", sf::Http::Request::Post);
with no success

httpd.conf settings show that it is listening at port 80
Code: [Select]
Listen 127.0.0.1:80

Even a third party application shows me that the server is listening to port 80.
« Last Edit: January 19, 2017, 09:02:49 am by Student555 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML help with web request
« Reply #10 on: January 19, 2017, 09:58:48 am »
So is this server running locally? If so try using 127.0.0.1 as IP for client.
If not, then you should be using 0.0.0.0 for your easy PHP server, so it can be accessed from outside.

I even tried
Code: [Select]
sf::Http::Request request("C:/Web/Practice/Project/LeaderBoards/Score.php", sf::Http::Request::Post);
with no success
Why would you think that this would do anything? You're not dealing with a file system path, but you're dealing with a web server path. They can be similar, but don't have to be.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: SFML help with web request
« Reply #11 on: January 19, 2017, 11:47:09 am »
Thanks exploiter it works now. It was just a matter of changing the IP address. Thanks again.

 

anything