微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 ESP8266 的 HTTP 获取请求

如何解决使用 ESP8266 的 HTTP 获取请求

为什么我无法发送获取请求。它总是响应400.URL没问题,它没有问题。实际上我需要使用 get 请求发送数据。我已尽力寻找解决方案,但我对此一无所知。请任何人都可以帮助我代码

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>//this header is used to send get request and see response
#include <WiFiClient.h>
const char* ssid = "myssid";
const char* password =  "mypswd";
String host_url="https://api-test.technofield.net";
String url1="/api/data?token=TEST_TOKEN_123&data=";
int httpPort=80;
void setup() {
 Serial.begin(9600);
WiFi.begin(ssid,password);     //Connect to my WiFi router
  Serial.println("");
  Serial.print("Connecting"); //initiall step just displaying connecting
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) { //the llop wiil execute till wifi is not connected
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);// connected to given ssid
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}


void loop(){
  if(WiFi.status()==WL_CONNECTED){ //we will perform action if wifi is connected
    HTTPClient http; //intialize object of HTTPclient which is importent
    String data="Sunil Kumar Yadav";
    //String token="TEST_TOKEN_123";
    url1=url1+data;
    //full APi url is created you can manipulate those above data and token vaiable.
    http.begin(host_url,httpPort,url1);//specify the request,begin the request 
    int httpCode=http.GET(); //Send the request
//    if (httpCode>0){ //lets see the response from server
//      String payload = http.getString();//store response on payload variable
//      Serial.print("==============");//just for seperating the wifi connection status and server response
//      Serial.println(payload);//print the response payload
//    }
  Serial.print(httpCode);
    http.end();//if there is begin there will be end,it will Close the connection
  }
  delay(500); //send the request in every 2sec,you can change it according to your need
  //you can also specify the time so that it will be easy to identify on which time data is send
}

以下是输出

301
==============<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>Nginx</center>
</body>
</html>

我不知道如何解决这个问题。

解决方法

这里有一个(多个)示例如何使用 HTTPS 执行此操作:https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>

const char* ssid = "myssid";
const char* password =  "mypswd";
String url = "https://api-test.technofield.net/api/data?token=TEST_TOKEN_123&data=";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid,password);
  Serial.println("");
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClientSecure client;
client.setInsecure();
    
    HTTPClient https;
    String data = "Sunil%20Kumar%20Yadav";
    String fullUrl = url + data;
    Serial.println("Requesting " + fullUrl);
    if (https.begin(client,fullUrl)) {
      int httpCode = https.GET();
      Serial.println("============== Response code: " + String(httpCode));
      if (httpCode > 0) {
        Serial.println(https.getString());
      }
      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }
  delay(5000);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。