From 0edab630286dc948ba9010566729bbe679e680de Mon Sep 17 00:00:00 2001 From: bestlqiang Date: Tue, 18 Mar 2025 17:56:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86HttpClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/HttpClient.cpp | 95 +++++++++++++++++++++++++++++++++++ client/HttpClient.h | 25 +++++++++ client/client.cpp | 54 ++------------------ client/client.vcxproj | 4 +- client/client.vcxproj.filters | 6 +++ 5 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 client/HttpClient.cpp create mode 100644 client/HttpClient.h diff --git a/client/HttpClient.cpp b/client/HttpClient.cpp new file mode 100644 index 0000000..fa7dc4e --- /dev/null +++ b/client/HttpClient.cpp @@ -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; +} diff --git a/client/HttpClient.h b/client/HttpClient.h new file mode 100644 index 0000000..ea33750 --- /dev/null +++ b/client/HttpClient.h @@ -0,0 +1,25 @@ +#pragma once +#define _AFXDLL +#include +#include +#include +#include +#include +#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; +}; + diff --git a/client/client.cpp b/client/client.cpp index 6ce1dce..f75864c 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -1,55 +1,11 @@ #include -#include -#include "../cpp-httplib/httplib.h" -#include -using namespace std; -using namespace httplib; +#include "HttpClient.h" -string GetExeDir() +int main() { - char buff[_MAX_PATH]; - _getcwd(buff, _MAX_PATH); - string currPath(buff); - return string(buff); -} - -const string CLIENT_FILE_DIR = "/ClientFiles/"; - -void download_file(const string& file_name) { - httplib::Client cli("http://localhost:1234"); - - auto res = cli.Get(("/download/" + file_name).c_str()); + CHttpClient cli("127.0.0.1", 1234); + cli.DownloadAllFile(); - if (res && res->status == 200) { - string save_path = GetExeDir()+CLIENT_FILE_DIR + file_name; - - // 打开文件进行写入 - ofstream file(save_path, ios::binary); - if (!file) { - cerr << "Failed to save file: " << save_path << endl; - return; - } - - file.write(res->body.c_str(), res->body.size()); - file.close(); - - cout << "File downloaded and saved at: " << save_path << endl; - } - else { - cerr << "Failed to download file. Status code: " - << (res ? to_string(res->status) : "No response") << endl; - } + getchar(); } -int main() { - string file_name; - - while (file_name!="q") - { - cout << "Enter the file name to download (q to Exit): "; - cin >> file_name; - download_file(file_name); - } - system("pause"); - return 0; -} diff --git a/client/client.vcxproj b/client/client.vcxproj index d2e0d59..a711a4f 100644 --- a/client/client.vcxproj +++ b/client/client.vcxproj @@ -29,7 +29,7 @@ Application true v140 - Unicode + MultiByte Application @@ -148,9 +148,11 @@ + + diff --git a/client/client.vcxproj.filters b/client/client.vcxproj.filters index c90e7cc..6861aaa 100644 --- a/client/client.vcxproj.filters +++ b/client/client.vcxproj.filters @@ -18,10 +18,16 @@ 婧愭枃浠 + + 婧愭枃浠 + 澶存枃浠 + + 澶存枃浠 + \ No newline at end of file