整理HttpClient

master
bestlqiang 4 months ago
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;
};

@ -1,55 +1,11 @@
#include <iostream>
#include <fstream>
#include "../cpp-httplib/httplib.h"
#include <direct.h>
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;
}

@ -29,7 +29,7 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
@ -148,9 +148,11 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="client.cpp" />
<ClCompile Include="HttpClient.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\cpp-httplib\httplib.h" />
<ClInclude Include="HttpClient.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

@ -18,10 +18,16 @@
<ClCompile Include="client.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="HttpClient.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\cpp-httplib\httplib.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="HttpClient.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading…
Cancel
Save