整理HttpClient
parent
9c2146ac04
commit
0edab63028
@ -0,0 +1,95 @@
|
|||||||
|
#include "HttpClient.h"
|
||||||
|
#define CLIENT_FILE_DIR "/ClientFiles";
|
||||||
|
#define CMD_GetFileList "GetFileList"
|
||||||
|
|
||||||
|
static string GetExeDir()
|
||||||
|
{
|
||||||
|
char buff[_MAX_PATH];
|
||||||
|
_getcwd(buff, _MAX_PATH);
|
||||||
|
string currPath(buff);
|
||||||
|
return string(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHttpClient::CHttpClient(string IP, int Port)
|
||||||
|
{
|
||||||
|
m_HostIP = IP;
|
||||||
|
m_HostPort = Port;
|
||||||
|
}
|
||||||
|
|
||||||
|
CHttpClient::~CHttpClient()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool CHttpClient::DownloadFile(const string& file_name)
|
||||||
|
{
|
||||||
|
httplib::Client cli(m_HostIP, m_HostPort);
|
||||||
|
auto res = cli.Get(("/download/" + file_name).c_str());
|
||||||
|
if (res && res->status == 200)
|
||||||
|
{
|
||||||
|
string save_path = GetExeDir() + CLIENT_FILE_DIR;
|
||||||
|
save_path += "/";
|
||||||
|
save_path += file_name;
|
||||||
|
// ´ò¿ªÎļþ½øÐÐдÈë
|
||||||
|
ofstream file(save_path, ios::binary);
|
||||||
|
if (!file)
|
||||||
|
{
|
||||||
|
cerr << "Failed to save file: " << save_path << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
file.write(res->body.c_str(), res->body.size());
|
||||||
|
file.close();
|
||||||
|
cout << "File downloaded and saved at: " << save_path << endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cerr << "Failed to download file. Status code: "
|
||||||
|
<< (res ? to_string(res->status) : "No response") << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CHttpClient::SendCmd(const string cmd, string & sRespon)
|
||||||
|
{
|
||||||
|
httplib::Client cli(m_HostIP,m_HostPort);
|
||||||
|
auto res = cli.Get(("/command/" + cmd).c_str());
|
||||||
|
|
||||||
|
if (res && res->status == 200)
|
||||||
|
{
|
||||||
|
sRespon = res->body;
|
||||||
|
cout << "Command Respon:" << sRespon << endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sRespon.clear();
|
||||||
|
string sErr = "SendCmd Failed. Status code: ";
|
||||||
|
sErr += res ? to_string(res->status) : "No response";
|
||||||
|
cerr << sErr << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CHttpClient::DownloadAllFile()
|
||||||
|
{
|
||||||
|
string sRespon;
|
||||||
|
if (!SendCmd(CMD_GetFileList, sRespon))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
CString FullStr = sRespon.data();
|
||||||
|
CString strNameValue; // an individual name, value pair
|
||||||
|
|
||||||
|
int i = 0; // substring index to extract
|
||||||
|
while (AfxExtractSubString(strNameValue, FullStr, i,','))
|
||||||
|
{
|
||||||
|
if (!strNameValue.IsEmpty())
|
||||||
|
{
|
||||||
|
DownloadFile(strNameValue.GetBuffer());
|
||||||
|
strNameValue.ReleaseBuffer();
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
#define _AFXDLL
|
||||||
|
#include <afxwin.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <direct.h>
|
||||||
|
#include "../cpp-httplib/httplib.h"
|
||||||
|
using namespace std;
|
||||||
|
using namespace httplib;
|
||||||
|
|
||||||
|
class CHttpClient
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CHttpClient(string IP, int Port);
|
||||||
|
virtual ~CHttpClient();
|
||||||
|
bool DownloadAllFile();
|
||||||
|
bool SendCmd(const string cmd, string & sRespon); //发送命令,返回值为Server响应(字符串)
|
||||||
|
bool DownloadFile(const string & file_name);
|
||||||
|
void SetRemote(string IP, int Port) {m_HostIP = IP;m_HostPort = Port; };
|
||||||
|
private:
|
||||||
|
string m_HostIP = "127.0.0.1";
|
||||||
|
int m_HostPort = 1234;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue