使用esp12F模块用arduino编程写一个代码获取代理ip为httpsabcliumangonedailiipiptxt的文本端口为httpsabcliumangonedailiipporttxt的文本实现访问httpsqyapiweixinqqcomcgi-binmessagesendaccess_token=tonken用post发送123用https连接使用代理IP连接
以下是可能的代码实现,但需要注意的是,代理IP的可用性可能会受到限制,而且这一实现方式并没有进行错误处理和异常情况的处理,需要根据实际情况进行修改和完善:
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* host = "qyapi.weixin.qq.com";
const int httpsPort = 443;
const char* token = "your_access_token";
const char* message = "123";
const char* proxyHost = "abc.liumang.one";
const int proxyPort = 443;
const char* proxyIpUrl = "/dailiip/ip.txt";
const char* proxyPortUrl = "/dailiip/port.txt";
WiFiClientSecure client;
WiFiClientSecure* proxyClient;
boolean connectProxy() {
String proxyIp;
String proxyPortStr;
if (client.connect(proxyHost, 443)) {
client.print(String("GET ") + proxyIpUrl + " HTTP/1.1\r\n" +
"Host: " + proxyHost + "\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line.indexOf("\r") >= 0) {
break;
}
if (line.startsWith("HTTP/1.1 ")) {
int statusCode = line.substring(9, 12).toInt();
if (statusCode != 200) {
return false;
}
}
if (line.startsWith("Content-Length: ")) {
int contentLength = line.substring(16).toInt();
proxyIp.reserve(contentLength);
}
}
while (client.available()) {
char c = client.read();
proxyIp += c;
}
client.stop();
} else {
return false;
}
if (client.connect(proxyHost, 443)) {
client.print(String("GET ") + proxyPortUrl + " HTTP/1.1\r\n" +
"Host: " + proxyHost + "\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line.indexOf("\r") >= 0) {
break;
}
if (line.startsWith("HTTP/1.1 ")) {
int statusCode = line.substring(9, 12).toInt();
if (statusCode != 200) {
return false;
}
}
if (line.startsWith("Content-Length: ")) {
int contentLength = line.substring(16).toInt();
proxyPortStr.reserve(contentLength);
}
}
while (client.available()) {
char c = client.read();
proxyPortStr += c;
}
client.stop();
} else {
return false;
}
IPAddress proxyIpAddr;
if (!proxyIpAddr.fromString(proxyIp)) {
return false;
}
int proxyPortInt = proxyPortStr.toInt();
proxyClient = new WiFiClientSecure();
proxyClient->setInsecure();
proxyClient->setProxy(WEBSOCKET, proxyIpAddr, proxyPortInt);
return true;
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
if (!connectProxy()) {
Serial.println("Failed to connect to proxy");
while (true) {}
}
}
void loop() {
if (!proxyClient->connect(host, httpsPort)) {
Serial.println("Failed to connect to server");
return;
}
String url = "/cgi-bin/message/send?access_token=";
url += token;
String postData = "{";
postData += "\"touser\":\"@all\",";
postData += "\"msgtype\":\"text\",";
postData += "\"agentid\":1000002,";
postData += "\"text\":{\"content\":\"";
postData += message;
postData += "\"},";
postData += "\"safe\":0";
postData += "}";
client.setTimeout(10000);
client.println(String("POST ") + url + " HTTP/1.1");
client.println(String("Host: ") + host);
client.println("Connection: close");
client.println("Content-Type: application/json");
client.println(String("Content-Length: ") + postData.length());
client.println();
client.println(postData);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line.indexOf("\r") >= 0) {
break;
}
}
String response = "";
while (client.available()) {
char c = client.read();
response += c;
}
client.stop();
Serial.println(response);
delay(5000);
}
原文地址: http://www.cveoy.top/t/topic/b2sn 著作权归作者所有。请勿转载和采集!