gpt4 book ai didi

c++ - 从 Arduino 以太网客户端 Get 中删除 header 信息

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:52:03 25 4
gpt4 key购买 nike

我想知道我是否遗漏了这个技巧。

在 arduino 上,你可以这样从网络服务中获取:

if (client.connect("google.com", 80)) {
client.println("GET /service/v2/time HTTP/1.1");
client.println("Host:nimbits-02.appspot.com");
client.println();
delay(1000);
while(client.connected() && !client.available()) delay(1);
while (client.available()) {
c = client.read();
Serial.print(c);
}

client.stop();
client.flush();

工作得很好(调用 nimbits 时间服务)

这个调用的content body就是我需要的,打印结果如上给我:

> HTTP/1.1 200 OK Date: Sat, 02 Feb 2013 17:24:38 GMT Content-Type:
> text/html Server: Google Frontend Content-Length: 13
>
> 1359825878036

一切都很好——但我必须在 arduino 上进行一些昂贵的字符串处理才能获取消息正文。我只想要 1359825878036。有没有办法告诉以太网客户端不要读取 header ?那会很方便。

到目前为止,我最好的解决方案是假设消息正文总是在最后一个换行符之后,这似乎很容易出错:

if (client.connect("google.com", 80)) {
client.println("GET /service/v2/time HTTP/1.1");
client.println("Host:nimbits-02.appspot.com");
client.println();
delay(1000);
while(client.connected() && !client.available()) delay(1);
while (client.available()) {
c = client.read();
response= response + c;
}
int contentBodyIndex = response.lastIndexOf('\n');
if (contentBodyIndex > 0) {
Serial.print(response.substring(contentBodyIndex));
}

client.stop();
client.flush();
}

谢谢,Ben - nimbits.com

最佳答案

他们是一把 key 。 header 以双 CRLF 结尾:

Response = Status-Line
*(( general-header
| response-header
| entity-header ) CRLF)
CRLF
[ message-body ]

参见 W3 doc

关于c++ - 从 Arduino 以太网客户端 Get 中删除 header 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com