如何使用ESP8266(Arduino核心)携带Cookie发送HTTP请求?
Hey Lani, great question! Using cookies to authenticate requests with an ESP8266 (Arduino core) is totally doable—here's a step-by-step breakdown to make it work for your YouTube channel info use case:
Before coding, you need to pull the active login cookies from your browser:
- Open the YouTube channel page you want to access in your desktop browser.
- Press
F12to open the developer tools, then go to the Application (Chrome) or Storage (Firefox) tab. - Under "Cookies", select the
youtube.comdomain. - Locate the authentication-related cookies (common ones include
LOGIN_INFO,SID, orHSID). Copy the full key-value pairs for these cookies (e.g.,LOGIN_INFO=abc123; SID=xyz789;).
Note: Cookies expire after a certain time, so you’ll need to refresh these values periodically if your ESP stops authenticating.
We’ll use the built-in ESP8266WiFi library for WiFi connection and ESP8266HTTPClient for sending HTTP requests with custom headers (including cookies). Make sure these libraries are installed via the Arduino Library Manager.
Here’s a complete, commented sketch that connects to WiFi, sends a GET request with your cookies, and prints the response:
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> // Replace with your WiFi credentials const char* ssid = "YOUR_WIFI_NAME"; const char* password = "YOUR_WIFI_PASSWORD"; // Replace with your target YouTube channel URL and cookies const char* targetUrl = "https://www.youtube.com/channel/YOUR_CHANNEL_ID/about"; const char* authCookies = "LOGIN_INFO=your_login_cookie; SID=your_sid_cookie;"; void setup() { Serial.begin(115200); delay(10); // Connect to WiFi Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected! IP: " + WiFi.localIP().toString()); } void loop() { // Run request every 60 seconds to avoid rate limiting if ((millis() % 60000) == 0) { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; // Initialize request with target URL http.begin(targetUrl); // Add authentication cookies to the request header http.addHeader("Cookie", authCookies); // Mimic a browser user-agent to avoid being blocked by YouTube http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"); // Send GET request int httpCode = http.GET(); // Handle response if (httpCode > 0) { Serial.printf("\nHTTP Status Code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); // Print first 500 characters of the response to avoid Serial overflow Serial.println("Response snippet:"); Serial.println(payload.substring(0, 500)); } } else { Serial.printf("Request failed: %s\n", http.errorToString(httpCode).c_str()); } // Close the HTTP connection http.end(); } else { Serial.println("WiFi disconnected—can't send request"); } } delay(1000); }
- Cookie Expiry: Keep an eye on when your cookies expire—you’ll need to re-copy fresh values from your browser if the ESP starts getting "unauthorized" responses.
- User-Agent Header: Sites like YouTube block requests with generic/empty user-agents. The example uses a modern Chrome user-agent to mimic a real browser.
- HTTPS Certificate Checks: If you hit SSL errors, you can add
trueto thehttp.begin()call (http.begin(targetUrl, true)) to skip certificate verification (not recommended for long-term use, but useful for testing). - Rate Limiting: Don’t send requests too frequently—YouTube may block your ESP’s IP if you spam requests. The 60-second delay in the example is a safe starting point.
内容的提问来源于stack exchange,提问作者Arduino Projects

