【问题标题】:ESP8266 send GET request to remote serverESP8266 向远程服务器发送 GET 请求
【发布时间】:2016-11-06 04:25:21
【问题描述】:

http://www.universalcard.byethost7.com 是我的服务器。我保存 index.php 文件的地方。代码如下所示

<?php
if(isset($_GET['username']) && isset($_GET['pin']) && isset($_GET['cost'])) {

$username = $_GET['username'];
$pin = $_GET['pin'];
$cost = $_GET['cost'];
$filecontent = "Username is: ".$username." and PIN is: ".$pin." and cost is: ".$cost."\n";
$filestatus = file_put_contents('uc.txt',$filecontent,FILE_APPEND);
if($filestatus != false )
{
    echo "Data written to file..";
}else{
    echo "Ohh sorry..";
}

} else {
     echo "Something went wrong..";
}
?>

我想用 Arduino IDE 从 ESP8266 发送一个 GET 请求。 在这个 GET 请求中,我发送了 3 个变量 'username' 、 'pin' 和 'cost' 以及一些值(数据类型是字符串)。这些值附加到文件“uc.txt”。因此,当我使用浏览器发送请求时,值将附加到文本文件中。

但是当我尝试使用 ESP8266 发送时,它没有附加

Arduino 代码如下

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "rainbow";
const char* password = "12345678";

const char* host = "universalcard.byethost7.com";
const int httpsPort = 443;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
//const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/index.php?username=2bv14is114&pin=5555&cost=1111";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
}

串行监视器中的输出如下

【问题讨论】:

  • 您的代码显示 byethehost7.com,但您的 cmets 显示 byethehost5.com 您是否将请求发送到错误的站点?
  • 是的,一个小错误应该是universalcard.byethost7.com。我现在将编辑问题。

标签: php esp8266 arduino-ide


【解决方案1】:

您的主机有某种保护(可能针对机器人),如果不存在的话,它需要一个由 JavaScript 设置的 _test cookie。

您可以通过首先使用浏览器访问该站点并将 cookie 复制粘贴到您的代码中来获取 cookie。
您需要从同一个 IP 执行此操作,您的 ESP8266 将被引入服务器,因为 cookie 是 IP 绑定的。
在这种情况下,如果您有一个动态 IP 并且 cookie 的寿命是未知的,那么您将遇到问题。

您也可以通过解析响应来获取 cookie,但 cookie 是 AES 加密的,这会有些复杂。

最明智的解决方案是切换到没有此类保护的主机。
这是thisthis问题中基本相同问题的解决方案。

【讨论】:

  • 是的,是主机的问题。我尝试使用另一个托管站点 www.hostinger.in,它运行良好。
  • 000webhostapp.com 是另一个经过测试的选项
猜你喜欢
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多