EasyDarwin use interface - interactive written in C ++ client server implementation and EasyDarwin

    EasyDarwin provides an interface for clients to use in order to achieve the client and their interactions. For example, according to the EasyDarwin interface documentation version 8.1.0, plug flow have acquired a list of interfaces as follows:

 

    We can test the above interfaces. First start EasyDarwin server, start the browser (such as QQ browser) Enter " http://127.0.0.1:10008/api/v1/pushers " (format: 'streaming media server where the computer's IP': ' easydarwin.ini http port number set in '+' interface message '), enter press enter, you can see the browser displays the following information. Because there is no push stream, the information presented in the following, total is 0, rows not. These show the response message is sent to your browser EasyDarwin server receives its "api / v1 / pushers" message is returned. We can see this interface is normal.

Next we get a list of plug flow as an example, to write C ++ client based on the interface. New console program in vs2015, enter the following code. Reference Code " C ++ implementation of a simple HTTP GET and POST requests class "

HttpRequest.h

#pragma once
#include <string>
#include <vector>

class HttpRequest
{
public:
	HttpRequest(const std::string& ip, int port);
	~HttpRequest(void);
	std::string HttpGet(std::string req);  //进行Http的GET请求
private:
	std::string m_ip;   //需要连接的http服务器的IP
	int m_port;         //需要连接的http服务器的端口号
};

 

HttpRequest.cpp

#include "HttpRequest.h"
#include <WinSock.h>
#include <iostream>

#pragma comment(lib, "ws2_32.lib")

HttpRequest::HttpRequest(const std::string& ip, int port) : m_ip(ip), m_port(port)
{
}


HttpRequest::~HttpRequest(void)
{
}

//进行Http的GET请求
std::string HttpRequest::HttpGet(std::string req)
{
	std::string ret = "";     //返回Http的Response(响应)
	try
	{
		/*进行socket初始化*/
		WSADATA wData;
		::WSAStartup(MAKEWORD(2, 2), &wData);

		SOCKET clientSocket = socket(AF_INET, 1, 0);
		struct sockaddr_in ServerAddr = { 0 };
		ServerAddr.sin_addr.s_addr = inet_addr(m_ip.c_str());
		ServerAddr.sin_port = htons(m_port);
		ServerAddr.sin_family = AF_INET;
		int errNo = connect(clientSocket, (sockaddr *)&ServerAddr, sizeof(ServerAddr));  //连接服务器
		if (0 == errNo)                            //如果成功连接上服务器
		{
			std::string strSend = " HTTP/1.1\r\n"
				"Host: 192.168.1.104:10008\r\n"
				"Connection: keep-alive\r\n"
				"Cache-Control: max-age=0\r\n"
				"Upgrade-Insecure-Requests: 1\r\n"
				"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3732.400 QQBrowser/10.5.3819.400\r\n"
				"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n"
				"Accept-Encoding: gzip, deflate\r\n"
				"Accept-Language: zh-CN,zh;q=0.9\r\n"
				"Cookie: token=42C0EN0ZR\r\n\r\n";

			strSend = "GET " + req + strSend;

			errNo = send(clientSocket, strSend.c_str(), strSend.length(), 0);   //发送缓冲区strSend中的信息给服务器
			if (errNo > 0)
			{
				std::cout << "发送成功" << std::endl;
			}
			else
			{
				std::cout << "发送失败" << std::endl;
				//std::cout << "errNo:" << errNo << std::endl;
				return ret;
			}

			int buf_size = 5000;
			char *bufRecv = new char[buf_size]();   //申请buf_size大小的空间并初始化为0,将指针bufRecv指向该空间
			errNo = recv(clientSocket, bufRecv, buf_size, 0);
			if (errNo > 0)           // 如果接收响应成功,则返回接收的数据内容
			{
				ret = bufRecv;        
				delete[] bufRecv;
			}
			else                     //如果接收响应失败
			{
				std::cout << "接收失败" << std::endl;
				delete[] bufRecv;
				//std::cout << "errNo:" << errNo << std::endl;
				return ret;
			}
		}
		else        //如果连接不上服务器
		{
			errNo = WSAGetLastError();
			std::cout << "errNo:" << errNo << std::endl;
		}
		// socket环境清理
		::WSACleanup();
	}
	catch (...)
	{
		return "";
	}
	return ret;
}

 

main.cpp

#include "HttpRequest.h"
#include <iostream>
#include <Windows.h>

int main(int argc, char *argv[])
{
	HttpRequest httpReq("127.0.0.1", 10008);
	std::string res = httpReq.HttpGet("/api/v1/pushers");
	std::cout << res << std::endl;
	return 0;
}

 

After entering the code completion, compiling, running, you can see the console output is as follows, prove that the client obtains a list of plug-flow EasyDarwin of success.

 

Here we must note a problem if the client, only the message "Get / api / v1 / pushers" to the server is unable to successfully get a response, and had to send HTTP headers. Wireshark client using the data sent by capture, data transmission can be seen as shown below, can be seen in addition to "Get / api / v1 / pushers", also made a lot of other things.

 

发布了54 篇原创文章 · 获赞 55 · 访问量 12万+

Guess you like

Origin blog.csdn.net/u014552102/article/details/102826521