#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define URL "http://xxxxx/xxxx"
// 设置wifi接入信息(请根据您的WiFi信息进行修改)
const char* ssid = "mark2";
const char* password = "xxx";
void setup() {
//初始化串口设置 注意波特率
Serial.begin(115200);
//设置ESP8266工作模式为无线终端模式,只能联网不能让其他连接
WiFi.mode(WIFI_STA);
//开始连接wifi
WiFi.begin(ssid, password);
//等待WiFi连接,连接成功打印IP
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi Connected!");
httpClientRequest();
}
void loop() {}
// 发送请求并且将响应通过串口输出
void httpClientRequest(){
WiFiClient client; //新添加
//1 创建 HTTPClient 对象
HTTPClient httpClient;
//2 通过begin函数配置请求地址。
httpClient.begin(client,URL);
Serial.print("URL: "); Serial.println(URL);
//3 通过GET函数发送HTTP请求
int httpCode = httpClient.GET();
Serial.print("Send GET request to URL: ");
Serial.println(URL);
//4. 响应状态码的输出
if (httpCode == HTTP_CODE_OK) {
// 使用getString函数获取服务器响应体内容
String responsePayload = httpClient.getString();
Serial.println("响应体: ");
Serial.println(responsePayload);
} else {
Serial.println("状态码:");
Serial.println(httpCode);
}
//5. 关闭客户端连接
httpClient.end();
}
01-06
08-31
563
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-15
9016
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)