我如何通过HTTP响应获取正文?

时间:2017-06-17 08:30:01

标签: http tcp arduino esp8266

嘿我正在做和Arduino项目,我想从HTTP请求获取正文,这里是代码:

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" +
             "Connection: close\r\n\r\n");
delay(100);
int liniea_info = 0;
while(client.available()){
    String line = client.readStringUntil('\r');
    if(liniea_info == 13){Serial.print(line);}
    Serial.print(liniea_info);
    ++liniea_info;
}

这很好用,没有整数liniea_info,请将此返回:

Requesting URL: /output/***.csv?colors=11
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Sat, 17 Jun 2017 08:09:31 GMT
Connection: close
Set-Cookie: SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Cache-control: private

34
colors,day,timestamp
11,12,2017-06-17T07:48:10.619Z

0

我认为使用int liniea_info我只会获得我想要的那一行"11,12,2017-06-17T07:48:10.619Z",但不会,只会打印第一行。

任何人都会看到我做错了什么或怎么做?

1 个答案:

答案 0 :(得分:1)

不要重新发明轮子。只需使用HTTPClient库。该库处理请求和响应。

添加到包含:

#include <ESP8266HTTPClient.h>

简单地说:

HTTPClient http;
http.begin("http://www.sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv");
int statusCode = http.GET();
Serial.println(http.getString());
http.end();