当前位置: 首页>>代码示例>>C++>>正文


C++ Tcp::println方法代码示例

本文整理汇总了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");
//.........这里部分代码省略.........
开发者ID:sapienzaapps,项目名称:galileo-terremoti,代码行数:101,代码来源:HTTPClient.cpp

示例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");
//.........这里部分代码省略.........
开发者ID:sapienzaapps,项目名称:galileo-terremoti,代码行数:101,代码来源:HTTPClient.cpp


注:本文中的Tcp::println方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。