本文整理汇总了C++中Tcp::println方法的典型用法代码示例。如果您正苦于以下问题:C++ Tcp::println方法的具体用法?C++ Tcp::println怎么用?C++ Tcp::println使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tcp
的用法示例。
在下文中一共展示了Tcp::println方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: fopen
HTTPResponse *HTTPClient::httpPostFile(std::string URL, std::string file) {
ssize_t fileSize = Utils::fileSize(file.c_str());
if (fileSize == -1) return NULL;
FILE *fp = fopen(file.c_str(), "r");
if (fp == NULL) {
// Woops!
return NULL;
}
uint8_t *buf = (uint8_t *) malloc(fileSize);
memset(buf, 0, (size_t) fileSize);
fread(buf, fileSize, 1, fp);
fclose(fp);
HTTPResponse *resp = new HTTPResponse();
Tcp client;
char serverName[100];
unsigned short serverPort = 80;
size_t pathOffset = hostFromURL(URL.c_str(), serverName, &serverPort);
if (client.connectTo(std::string(serverName), serverPort)) {
Log::d("Connect to server OK");
char linebuf[1024];
snprintf(linebuf, 1024, "POST %s HTTP/1.1", URL.c_str() + pathOffset);
client.println(linebuf);
if (serverPort != 80) {
snprintf(linebuf, 1024, "Host: %s:%i", serverName, serverPort);
} else {
snprintf(linebuf, 1024, "Host: %s", serverName);
}
client.println(linebuf);
client.println("Content-Type: application/octet-stream");
client.println("Connection: close");
std::string contentLength = "Content-Length: " + Utils::toString((int) fileSize);
client.println(contentLength.c_str());
client.println("");
client.send(buf, fileSize);
Log::d("HTTP Request to %s sent", URL.c_str());
// Request sent, wait for reply
unsigned long reqTime = Utils::millis();
while (!client.available() && (Utils::millis() - reqTime < HTTP_RESPONSE_TIMEOUT_VALUE)) {
#ifndef NOWATCHDOG
Watchdog::heartBeat();
#endif
}
if (client.available()) {
char rBuffer[300 + 1];
memset(rBuffer, 0, 300 + 1);
int s = getLine(client, (uint8_t *) rBuffer, 300);
Log::d("buffer response[%i]: %s", s, rBuffer);
if (strncmp(rBuffer, "HTTP/1.", 7) == 0) {
resp->error = HTTP_OK;
resp->responseCode = getResponseCode(rBuffer);
// Read headers
do {
s = getLine(client, (uint8_t *) rBuffer, 300);
if (s > 0 && strlen(rBuffer) != 0) {
char *dppos = strchr(rBuffer, ':');
*dppos = 0;
if (*(dppos + 1) == ' ') {
dppos++;
}
dppos++;
resp->headers[std::string(rBuffer)] = std::string(dppos);
}
} while (s > 0 && strlen(rBuffer) != 0);
resp->body = NULL;
if (resp->headers.count("Content-Length") == 1) {
size_t bodySize = (size_t) atol(resp->headers["Content-Length"].c_str());
resp->body = (uint8_t *) malloc(bodySize + 1);
memset(resp->body, 0, bodySize + 1);
client.readall(resp->body, bodySize);
}
} else {
Log::e("HTTP malformed reply");
resp->error = HTTP_MALFORMED_REPLY;
}
} else {
Log::e("HTTP request timeout");
resp->error = HTTP_REQUEST_TIMEOUT;
}
} else {
Log::e("HTTP connection timeout");
resp->error = HTTP_CONNECTION_TIMEOUT;
}
free(buf);
Log::d("Stopping tcp client");
//.........这里部分代码省略.........
示例2: HTTPResponse
HTTPResponse *HTTPClient::httpRequest(HTTPMethod method, std::string URL,
std::map<std::string, std::string> postValues) {
HTTPResponse *resp = new HTTPResponse();
Tcp client;
char serverName[100];
unsigned short serverPort = 80;
size_t pathOffset = hostFromURL(URL.c_str(), serverName, &serverPort);
if (client.connectTo(std::string(serverName), serverPort)) {
Log::d("Connect to server OK");
char linebuf[1024];
snprintf(linebuf, 1024, "%s %s HTTP/1.1", (method == HTTP_GET ? "GET" : "POST"), URL.c_str() + pathOffset);
client.println(linebuf);
if (serverPort != 80) {
snprintf(linebuf, 1024, "Host: %s:%i", serverName, serverPort);
} else {
snprintf(linebuf, 1024, "Host: %s", serverName);
}
client.println(linebuf);
client.println("Connection: close");
if (method == HTTP_POST && postValues.empty()) {
client.println("Content-Length: 0");
client.println("");
} else if (method == HTTP_POST && !postValues.empty()) {
std::string reqBody;
for (std::map<std::string, std::string>::iterator i = postValues.begin(); i != postValues.end(); ++i) {
reqBody.append(i->first);
reqBody.append("=");
reqBody.append(i->second);
reqBody.append("&");
}
reqBody = Utils::trim(reqBody, '&');
client.println("Content-Type: application/x-www-form-urlencoded");
unsigned long contentLength = reqBody.size();
snprintf(linebuf, 1024, "Content-Length: %lu", contentLength);
client.println(linebuf);
client.println("");
client.print(reqBody.c_str());
Log::d("HTTP Post: %s", reqBody.c_str());
}
Log::d("HTTP Request to %s sent", URL.c_str());
// Request sent, wait for reply
unsigned long reqTime = Utils::millis();
while (!client.available() && (Utils::millis() - reqTime < HTTP_RESPONSE_TIMEOUT_VALUE)) { ; }
if (client.available()) {
char rBuffer[300 + 1];
memset(rBuffer, 0, 300 + 1);
int s = getLine(client, (uint8_t *) rBuffer, 300);
Log::d("buffer response[%i]: %s", s, rBuffer);
if (strncmp(rBuffer, "HTTP/1.", 7) == 0) {
resp->error = HTTP_OK;
resp->responseCode = getResponseCode(rBuffer);
// Read headers
do {
s = getLine(client, (uint8_t *) rBuffer, 300);
if (s > 0 && strlen(rBuffer) != 0) {
char *dppos = strchr(rBuffer, ':');
*dppos = 0;
if (*(dppos + 1) == ' ') {
dppos++;
}
dppos++;
resp->headers[std::string(rBuffer)] = std::string(dppos);
}
} while (s > 0 && strlen(rBuffer) != 0);
resp->body = NULL;
if (resp->headers.count("Content-Length") == 1) {
size_t bodySize = (size_t) atol(resp->headers["Content-Length"].c_str());
resp->body = (uint8_t *) malloc(bodySize + 1);
memset(resp->body, 0, bodySize + 1);
client.readall(resp->body, bodySize);
}
} else {
Log::e("HTTP malformed reply");
resp->error = HTTP_MALFORMED_REPLY;
}
} else {
Log::e("HTTP request timeout");
resp->error = HTTP_REQUEST_TIMEOUT;
}
} else {
Log::e("HTTP connection timeout");
resp->error = HTTP_CONNECTION_TIMEOUT;
}
Log::d("Stopping tcp client");
//.........这里部分代码省略.........