简体   繁体   English

如何从HTTP响应获取正文?

[英]How I can get the body from a HTTP response?

Hey I am doing and Arduino project, and I want to get the body from an HTTP request, here is the code: 嘿,我正在做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;
}

This works good, without the integer liniea_info returns me this: 这很好用,没有整数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

I thought that with the int liniea_info I will get only the line that I want that is the line "11,12,2017-06-17T07:48:10.619Z" , but no, with this only prints the first line. 我认为使用int liniea_info只会得到我想要的行,即"11,12,2017-06-17T07:48:10.619Z" ,但是不行,仅打印第一行。

Anyone sees what I am doing wrong or how to do it? 有人看到我在做什么错或如何做吗?

Don't reinvent the wheel. 不要重新发明轮子。 Just use HTTPClient library. 只需使用HTTPClient库。 This library handles request and response. 该库处理请求和响应。

Add to includes: 添加到包括:

#include <ESP8266HTTPClient.h>

And simply: 并简单地:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止Angular $ http反序列化响应正文? - How can I stop Angular $http from deserializing the response body? 如何在Python中从包含整个响应的字符串中获取http响应的主体? - How do I get the body of a http response from a string containing the entire response, in Python? 当 java 中的请求失败时,如何获取 http 响应正文? - How can I get http response body when request is failed in java? 如何使用restTemplate获取真正的http代码和响应正文? - How can I get real http code and response body using restTemplate? 如何从 response.body 获取标题/项目(HTTP - JSON) - How to get title/item from response.body (HTTP - JSON) 我可以使用 tcpdump 来获取 HTTP 请求、响应头和响应正文吗? - Can I use tcpdump to get HTTP requests, response header and response body? 如何使用 chromedp 获取 HTTP 响应正文? - How to get the HTTP response body using chromedp? 如何在 Angular 中的 HTTP GET 请求中发送正文中的值? - How can I send values in the body in the HTTP GET request in Angular? 我可以从 http module.get 响应中获取 nodejs 中的完整 http 响应文本吗? - Can I get the full http response text in nodejs from a http module .get response? 如何从此示例HTTP请求获取正文? - How do I get the body from this example HTTP request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM