问题描述
我有一个文件下载服务器(称为 myfilesserver.xyz )使用http (不是https),我想从文件中下载文件。在python中,我有一个简单的2行代码片段即可完成此操作(请参见下文)。但是在C ++ 上,我不能只用HTTP请求来完成它,它返回一个空响应。
我还没有在Google上找到解决方案,有人知道(紧凑的首选方式)下载文件并以字符串形式读取内容吗?>
Python代码
import requests
x = requests.get("http://fileserver/release/file.bin")
P.S。我已经为此使用了一个库,它是大多数Arduino附带的HTTPClient。我不是在寻找新的。我想知道是否有可能这样做。
在将代码发布到这里之前,我不会删节代码,因为上次我被告知不要这样做。如果我错了,请告诉我。
我当前正在使用的代码:
void update(String repoUrl) {
HTTPClient http;
http.begin(repoUrl);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
payload = http.getString().substring(22);
} else {
Serial.println("Wrong HTTP Response: " + httpCode);
return;
}
} else {
Serial.println("Wrong HTTP Response: " + httpCode);
return;
}
http.begin(payload);
httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
payload = http.getString().substring(22);
} else {
Serial.println("Wrong HTTP Response: " + httpCode);
return;
}
} else {
Serial.println("Wrong HTTP Response: " + httpCode);
return;
}
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS!");
return;
}
File fileWrite = SPIFFS.open("/update.bin",FILE_WRITE);
if(!fileWrite){
Serial.println("An error has occured while opening the file for writing!");
return;
}
String fw = http.getString();
Serial.println(fw);
fileWrite.print(fw);
Serial.println("File should be written,size is " + fileWrite.size());
fileWrite.close();
File fileRead = SPIFFS.open("/update.bin");
if (fileRead) {
if(fileRead.isDirectory()){
Serial.println("Error,update.bin is not a file");
fileRead.close();
return;
}
size_t fileSize = fileRead.size();
if (fileSize > 0) {
Serial.println("Try to start update");
if (Update.begin(fileSize)) {
size_t written = Update.writeStream(fileRead);
if (written == fileSize) {
Serial.println("Written : " + String(written) + " successfully");
} else {
Serial.println("Written only : " + String(written) + "/" + String(fileSize) + ". Retry?");
}
if (Update.end()) {
Serial.println("OTA done!");
if (Update.isFinished()) {
Serial.println("Update successfully completed. Rebooting.");
} else {
Serial.println("Update not finished? Something went wrong!");
}
} else {
Serial.println("Error Occurred. Error #: " + String(Update.getError()));
}
} else {
Serial.println("Not enough space to begin OTA");
}
} else {
Serial.println("Error,file is empty");
}
fileRead.close();
// when finished remove the binary from sd card to indicate end of the process
SPIFFS.remove("/update.bin");
} else {
Serial.println("Could not load update.bin from sd root");
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)