对来自 Seeed Wio 终端的 GET 请求没有响应

问题描述

Wio 终端对特定 API api.ambeedata.com 的 GET 请求没有响应。

代码修改自 Seeed 的示例:从 Wio 终端读取 Github 存储库统计信息。最初的 Github 示例工作正常。

修改代码以传递我的 API 密钥并使用 api.ambeedata.com 端点后,请求没有返回。但是,与服务器建立了连接。

使用相同的请求在 Postman 中测试请求返回正确的 JSON 响应。

Realtek RTL8720 固件为 2.1.2,所有依赖库均为最新版本。 (Arduino FS、rpcUnified、rpcWiFi、SFUD、mbedtls)

使用不同的 Arduino 板,我复制了代码并从 api.ambeedata.com 获得了正确的 JSON 响应。任何有关下一步尝试的建议将不胜感激!

#include <rpcWiFi.h>
#include <WiFiClientSecure.h>

const char* ssid     = "SSID";     // your network SSID
const char* password = "password"; // your network password

const char* server = "api.ambeedata.com";  // Server URL
const char* test_root_ca = \
                           "-----BEGIN CERTIFICATE-----\n"
                           //Removed for the brevity of the post
                           "-----END CERTIFICATE-----\n";

WiFiClientSecure client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial); // Wait for Serial to be ready
  delay(1000);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    Serial.println(WiFi.status());
    WiFi.begin(ssid,password);
    Serial.println(WiFi.status());
    Serial.print(".");
    // wait 3 seconds for re-trying
    delay(3000);
    }
  
  Serial.print("Connected to ");
  Serial.println(ssid);

  client.setCACert(test_root_ca);

  Serial.println("\nStarting connection to server...");
  
  if (!client.connect(server,443)) {
    Serial.println("Connection Failed!");
    } 
  else {
    Serial.println("Connected to server!");
     
  //Send GET request to API
    client.println("GET https://api.ambeedata.com HTTP/1.0");
    client.println("Host: api.ambeedata.com");
    client.println("Connection: close");
    client.println("x-api-key: apikey");
    client.println("Content-type: application/json");
    client.println();

    Serial.println("request sent");

    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
  Serial.println("reply was:");
  Serial.println("==========");
  
  String line = client.readStringUntil('\n');
        if (line == "\r") {
            Serial.println("headers received");
        }
  
    // if there are incoming bytes available
    // from the server,read them and print them:
    while (client.available()) {
      char c = client.read();
      if (c == '\n') {
        Serial.write('\r');
      }
      Serial.write(c);
    }
    client.stop();
  }
}

void loop() {
  // do nothing
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)