本文整理汇总了C++中HTTPClient::getStreamPtr方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPClient::getStreamPtr方法的具体用法?C++ HTTPClient::getStreamPtr怎么用?C++ HTTPClient::getStreamPtr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPClient
的用法示例。
在下文中一共展示了HTTPClient::getStreamPtr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connect
/********************************************************************
connect establishes a connection to the Slack RTM API
********************************************************************/
bool ArduinoSlackBot::connect() {
// Step 1: Find WebSocket address via RTM API (https://api.slack.com/methods/rtm.start)
HTTPClient http;
String slackAddr = "https://slack.com/api/rtm.start?token=";
slackAddr = String(slackAddr + slackToken);
PRINTLN(slackAddr);
http.begin(slackAddr.c_str(), slackSSLFingerprint);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
PRINTF("HTTP GET failed with code %d\n", httpCode);
return false;
}
WiFiClient *client = http.getStreamPtr();
client->find("wss:\\/\\/");
String host = client->readStringUntil('\\');
String path = client->readStringUntil('"');
path.replace("\\/", "/");
// Step 2: Open WebSocket connection and register event handler
PRINTLN("WebSocket Host=" + host + " Path=" + path);
webSocket.beginSSL(host, 443, path, "", "");
webSocket.onEvent(webSocketEvent);
return true;
}
示例2: uploadFile
int uploadFile(const char* _filename, const char* _fileurl) { // upload new file to fs by downloading from a remote server, rather than reflash the entire spiffs
int ret = false;
HTTPClient http;
//const char* fileUrl = "http://mypi3/iot/index.html";
//const char* fileName = "/test2.html";
if (hasSerial) Serial.printf("url %s\n", _fileurl);
if (hasSerial) Serial.printf("file %s\n", _filename);
http.begin(_fileurl); // init http client
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// file found at server
if(httpCode == HTTP_CODE_OK) {
if (hasSerial) Serial.printf("HTTP client http status %d\n", httpCode);
// get lenght of document (is -1 when Server sends no Content-Length header)
int len = http.getSize();
int paysize = len;
if (hasSerial) Serial.printf("HTTP content size %d bytes\n", paysize);
// create buffer for read
uint8_t buff[128] = { 0 };
// get tcp stream
WiFiClient * stream = http.getStreamPtr();
// create or recreate file on spiffs
File configFile = SPIFFS.open(_filename, "w");
if (!configFile) {
if (hasSerial) Serial.printf("Failed to open %s for write.\n",_filename);
return ret;
}
if (hasSerial) Serial.println("File open, write start.");
// read all data from server
while(http.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = stream->available();
if (size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
// write it to Serial
configFile.write(buff, c);
if(len > 0) {
len -= c;
}
}
} // EoF or http connection closed
configFile.close();
http.end();
if (hasSerial) Serial.println("File closed, write complete.");
return paysize;
} else {
if (hasSerial) Serial.printf("HTTP client http error %d\n", httpCode);
return httpCode;
}
} else {
if (hasSerial) Serial.printf("HTTP client http error %d\n", httpCode);
return httpCode;
}
return 0;
}