0

I'm trying to get html source after login a website with a userdata, but it doesn't give me html source. So where is the problem in my source code?On the website it redirects me to a server select page after login "lobby.ogame.gameforge.com/?language=tr"; I'm trying to get html source of this page.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string URI = "https://tr.ogame.gameforge.com/";
        string myParameters = "login=testusername&pass=testpassword";

        using (WebClient wc = new WebClient())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string HtmlResult = wc.UploadString(URI, myParameters);
            richTextBox1.Text = HtmlResult;
        }
    }
}
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
  • What you mean by "doesn't give"? Is it an error, is it an empty string, is it not full html that you expect? – Andrius Naruševičius Aug 07 '18 at 18:53
  • on the website it redirects me to a server select page after login "https://lobby.ogame.gameforge.com/?language=tr" I'm trying to get html source of this page on c# but it just gives me first 4 5 lines of html source.Maybe login is failed on code I don't know. @AndriusNaruševičius – RJ Otome HD Aug 07 '18 at 18:57
  • So which page you want to download? The login one or the lobby? – Andrius Naruševičius Aug 07 '18 at 19:04
  • I updated the source code now I'm using Webclient and trying to get lobby page html source @AndriusNaruševičius – RJ Otome HD Aug 07 '18 at 19:09

2 Answers2

0

Your main problem is that you will need the javascript to run to get the data and you will not be able to do that with just DownloadString, etc. - that is, you need to mimic a web browser. I would advise getting a browser component like gecko, then look how to post the data into it here. Then, once the list is downloaded and properly filled, you can access the current html document.

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
0

Logging in

I looked at the website and how its login system works and you are making a couple of assumptions as to how it works that are incorrect. The way you log in to this specific website is by sending a request to "https://lobby-api.ogame.gameforge.com/users" and giving it data in the "application/x-www-form-urlencoded" format. The data needed is as shown in the following table:

Key █ Value
credentials[email] █ the email here
credentials[password] █ the password here

Once you send this request you will receive a cookie called "PHPSESSID" You can use this cookie to make subsequent requests, for example, to "https://lobby.ogame.gameforge.com/?language=tr" which is the page you are trying to get to when going to "index.php"

More issues

However, once you do load this page and render the HTML you will find that it does not contain anything interesting like the servers which is probably what you are after. Here is the HTML:

<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
      <meta name="theme-color" content="#000000">
      <link rel="shortcut icon" href="/favicon.ico">
      <script type="text/javascript" src="/config/configuration.js"></script>
      <title>OGame Lobby</title>
      <link href="https://s3-static.geo.gfsrv.net/browsergamelobby/ogame/1.0.8/css/main.2e4c281d.css" rel="stylesheet">
   </head>
   <body>
      <noscript>You need to enable JavaScript to run this app.</noscript>
      <div id="root"></div>
      <div class="planet"></div>
      <script type="text/javascript" src="https://s3-static.geo.gfsrv.net/browsergamelobby/ogame/1.0.8/js/main.edde2ed8.js"></script>
   </body>
</html>

The javascript then loads makes the thing on the page. This leaves you two options, you could use a browser component as suggested by Andrius Naruševičius or you could use the API that the javascript uses. In order to figure out the API, you could use Network tab in your browser's dev tools. This way may be more complicated initially but in the end, it should be easier and make for cleaner code because the API is designed to be used by people (the people who made it) but the HTML was not designed to be parsed because it was made for the browser, not a human. However, depending on what you intend to do with the list of servers it might actually be easier to use Andrius's way, you will have to make that decision for yourself.

How to go on if you choose to go my route

You can learn about the chrome dev tools network tab here and by using google (obviously). You can test your API calls using a software like Postman. If you know nothing about web requests/APIs, cookies, and session IDs you should not start here, you should learn what those are first. To learn that just look them up on Google.