使用ESP8266连接到强制门户wifi

时间:2015-10-26 10:18:56

标签: arduino wifi captivenetwork esp8266

我想在一个受强制门户保护的wifi网络上连接基于ESP8266的传感器(我没有别的选择,我不能要求减损)。 我有一个登录/密码来连接。

从基本的计算机上,当我连接到网络并进行互联网请求时(例如,我搜索" bl"在谷歌上),我得到了这样一个页面:{ {3}}

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
    ...
    </style>
    <body>
<div class="oc">
  <div class="ic">
    <form action="/" method="post">
      <input type="hidden" name="4Tredir" value= "https://www.google.com/search?q=test&ie=utf-8&oe=utf-8">
      <input type="hidden" name="magic" value="12291a0aff04200a">
      <input type="hidden" name="answer" value="0">
      <h1 class="logo">
        GENERAL CONDITIONS
      </h1>
      <p>
      I.    OBJET <br /> <br />
      Some blabla..
      </p>
      <h2>
        Do you agree to the above terms?
      </h2>
      <div class="fec">
        <input type="submit" value= "Yes, I agree" onclick="sb('1')">
        <input type="submit" value= "No, I decline" onclick="sb('0')">
      </div>
    </form>
   </div>
   </div>
   <script>
     function sb(val) {
       document.forms[0].answer.value = val;
       document.forms[0].submit();
      }
    </script>
  </body>
</html>

因此,我们在此页面中看到,我们获得了一个“魔法值”#34;这实际上是会话的ID。当我点击“同意”按钮时,我会看到此页面https://url:1003/fgtauth?12291a0aff04200a

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
    ...
    </style>

    <title>
      Firewall Authentication
    </title>
  </head>
  <body>
    <div class="oc">
      <div class="ic">
        <form action="/" method="post">
        <input type="hidden" name="4Tredir" value= "https://www.google.com/search?q=bl&ie=utf-8&oe=utf-8">
        <input type="hidden" name="magic" value="122713150676bec1">
        <h1 class="logo">
          Authentication Required
        </h1>
      <h2>
        Please enter your username and password to continue.
      </h2>
        <div class="fer">
          <label for="ft_un">
            Username:
          </label>
            <input name="username" id="ft_un" style="width:245px">
            <br>
          </div>
          <div class="fer">
            <label for="ft_pd">
              Password:
            </label>
            <input name="password" id="ft_pd" type="password" style="width:245px">
          </div>
          <div class="fer">
            <input type="submit" value= "Continue">
          </div>
        </form>
      </div>
    </div>
  </body>
</html>

在这里,我填写用户名和密码,然后将它们发送到服务器,该服务器返回一个空白页面,然后按OK。

所以,我想从ESP8266做这一步。我分两步看到:

  • 申请页面
  • 获得结果并存储魔法
  • 假冒&#34;同意&#34;请求页面
  • 假a&#34; user / id / magic&#34;请求页面

可以在此处找到ESP8266请求页面的示例: https://url:1003/ 我们在这里看到,我们可以发送POST请求:

client.print("POST /update HTTP/1.1\n");

这里有一个很好的解析页面的例子:https://github.com/iobridge/ThingSpeak-Arduino-Examples/blob/master/Ethernet/Arduino_to_ThingSpeak.ino

所以,我可能会这样做并发布答案,但首先我需要一些关于如何创建上述&#34;假的&#34;请求。

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

ESP8266能够使用HTTPS,实际上最新的ESP8226 Arduino代码库包含一个用于连接HTTPS服务器的WiFiClientSecure类。

假设此页面永远不会改变,并且您正在使用Arduino环境,则需要编写一些基本功能。其中一个将是您已经链接的初始GET函数,并检查您是否收到了您想要的页面,或者您是否被定向到门户网站。如果您被定向到门户网站,则需要编写等效的POST函数来发送“我同意”响应。

现在,这个POST函数将要求您将HTTP表单编码的有效负载发送到包含“Answer”值的服务器 - 您可以在此处阅读http://www.w3.org/TR/html401/interact/forms.html#h-17.13。由于您(可能)不希望页面发生变化,因此您可以对其进行硬编码:

client.println("POST / HTTP/1.1");
// $PORTAL_HOST should be the host of the captive portal, e.g. 10.1.1.1
client.println("Host: $PORTAL_HOST");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: 8");
client.print("\n");
client.print("Answer=1");

发送有效负载后,您应该会收到您的辅助用户/密码页面。从那里可以通过indexOf / substring或类似的方式提取魔术/会话,然后使用您现在提取的数据(如果需要)重复上述代码。

如果您需要更好地了解将要发送的内容,我建议您在浏览器上打开调试/网络选项卡,并观看浏览器发送到此门户页面时发送的确切数据。您将希望尽可能地模仿它。

相关问题