ASP.NET Core + ESP32 HTTP请求,GET返回代码200,但内容不可读

时间:2019-03-26 14:53:53

标签: arduino esp32

我需要从ESP32控制器向我的ASP.NET Core Server发出get请求。问题是,请求成功,但是我无法读取该字符串,原因是该字符串包含奇怪的字符(下图)

enter image description here

这是我的代码:

#include <Arduino.h>
#include "WiFi.h"
#include "HTTPClient.h"

const char* ssid = "ssid";
const char* password = "pass";

void setup() {

  Serial.begin(9600);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");  

}

void loop() {

  if ((WiFi.status() == WL_CONNECTED)) { 

    HTTPClient http;   
    http.begin("http://172.16.1.146:45455/api/controller/getlastresult/1");

    int httpCode = http.GET();                                        

    if (httpCode == 200) {
        String payload = http.getString();    
        Serial.println(httpCode);
        Serial.println(payload);
      }     
    else {
      Serial.println("Error on HTTP request");
    }

    http.end();
  }

  delay(1000);

}

我注意到,如果我更改了网址,例如将https://httpbin.org/get放在此处,则效果很好

enter image description here

所以,我想问题出在我的ASP.NET Core代码中。这是此请求的代码:

    [HttpGet]
    [Route("api/controller/getlastresult/{id}")]
    public IActionResult GetLatestResultForController(string id)
    {
        var list = _dbAccessLayer.GetAllVoteResults();
        var appropriateResults = list.Where(item => item.DeviceId.Equals(id) && item.Status == "Finished")
            .OrderBy(item => item.TimeStarted);

        if (appropriateResults.Any())
            return Ok(appropriateResults.Last().Result);

        return BadRequest();
    }

在邮递员中,这很好用,所以我很困惑

enter image description here

希望,您可以帮助我,谢谢

0 个答案:

没有答案