json - 在 HTTP 响应中解析 JSON 文档 - Arduino(意外响应:HTTP/1.1 301 永久移动错误)
问题描述
我正在使用 Arduino 的以太网屏蔽和 Arduino mega 来解析 HTTP 响应中的 JSON 文档,以读取 JSON 文件中的一些值。
JSON 地址为:https ://api.coinbase.com/v2/prices/BTC-USD/spot 。
我收到此错误消息:
连接...连接!意外响应:HTTP/1.1 301 永久移动
是不是 Arduino 库没有 SSL 功能,所以它无法连接到安全服务器?如果是,那么我需要你的帮助来解决这个问题。或者我可能需要使用另一个与 https 相关的端口?
这是我的代码:
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
void setup() {
// Initialize Serial port
Serial.begin(9600);
while (!Serial) continue;
// Initialize Ethernet library
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
if (!Ethernet.begin(mac)) {
Serial.println(F("Failed to configure Ethernet"));
return;
}
delay(1000);
Serial.println(F("Connecting..."));
// Connect to HTTP server
EthernetClient client;
client.setTimeout(10000);
if (!client.connect("api.coinbase.com", 80)) {
Serial.println(F("Connection failed"));
return;
}
Serial.println(F("Connected!"));
// Send HTTP request
client.println(F("GET /v2/prices/BTC-USD/spot/response.json HTTP/1.0"));
client.println(F("Host: api.coinbase.com"));
client.println(F("Connection: close"));
if (client.println() == 0) {
Serial.println(F("Failed to send request"));
return;
}
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
// It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
if (strcmp(status + 9, "200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
return;
// the rest of the code related to parsing that I didn't paste for the sake the argument.
谢谢大家。
解决方案
推荐阅读
- ios - 在 iOS 上为 Tensorflow Lite 构建静态框架
- angular - Angular 在文本中获得点击的单词
- windows - 如何在 Windows 10 上使用 IPC::Run 捕获超时异常?
- angular - 将字体库导入 Angular 项目
- python-3.x - Plot a selected range of co-ordinates using x-aaray
- excel - 如何将范围中最近的非连续单元格复制到另一个工作表上的连续范围中?
- lua - IntValue 始终等于 0,即使它不是
- count - 检查按下**特定**键的次数 - pygame
- r - defaultCluster(cl) 中的错误:未提供集群“cl”且未注册
- c++ - 将随机整数添加到总数中?