问题标题: 在线数据存储的C++方法

0
0
已解决
包思远
包思远
新手启示者
新手启示者

上午花了1h写成的,可能不够完善,请大家见谅(后面国庆还会更新)。

附上代码:

#include<bits/stdc++.h>
#include<windows.h>
#include<curl/curl.h>
using namespace std;
size_t write_callback_string(void* contents, size_t size, size_t nmemb, void* userp) {
	std::string* str = (std::string*)userp;
	size_t totalSize = size * nmemb;
	str->append((char*)contents, totalSize);
	return totalSize;
}
string UtfToGbk(const char* utf8){
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len+1];
	memset(str, 0, len+1);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return str;
}
string GbkToUtf(const char* strGBK) {
	string amp;
	int len = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0); // 获取目标宽字符数组的大小
	wchar_t* wszUtf8 = new wchar_t[len+1];
	memset(wszUtf8, 0, len * 2 + 2); // 初始化宽字符数组
	MultiByteToWideChar(CP_ACP, 0, strGBK, -1, wszUtf8, len); // 执行转换
	len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); // 获取目标多字节数组的大小
	char *szUtf8 = new char[len + 1]; // 分配多字节数组
	memset(szUtf8, 0, len + 1); // 初始化多字节数组
	WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL, NULL); // 执行转换
	amp = szUtf8; // 更新输入字符串
	delete[] szUtf8; // 释放内存
	delete[] wszUtf8; // 释放内存
	return amp;
}
string createoltext(CURL* curl,string key,string value){
	CURLcode res;
	std::string response;

	std::string apiUrl = "https://api.textdb.online/update/?key="+key+"&value="+GbkToUtf(value.c_str());

	curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

	struct curl_slist *headers = NULL;
	headers = curl_slist_append(headers, "Content-Type: text/plain");
	headers = curl_slist_append(headers, "User-Agent: Chrome");
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

	curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");

	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_string);
	
	res = curl_easy_perform(curl);
	
	if (res != CURLE_OK) {
		return "error";
	}
	return response.c_str();
}
string deleteoltext(CURL* curl,string key){
	return createoltext(curl,key,"");
}
string getoltext(CURL* curl,string key){
	CURLcode res;
	std::string response;

	std::string apiUrl = "https://textdb.online/"+key;

	curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

	struct curl_slist *headers = NULL;
	headers = curl_slist_append(headers, "Content-Type: text/plain");
	headers = curl_slist_append(headers, "User-Agent: Chrome");
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_string);
	
	res = curl_easy_perform(curl);
	
	if (res != CURLE_OK) {
		return "error";
	}
	return UtfToGbk(response.c_str());
}
CURL* curl_init(){
	CURL* curl=curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
	return curl;
}
int main(){
	CURL* curl=curl_init();
	createoltext(curl,"123456","数据在线存储");
	cout<<getoltext(curl,"123456");
	return 0;
}

 

包思远在2024-09-28 13:35:50追加了内容

注:大家可能运行不了这个代码(后面会写exe以供调用使用)

包思远在2024-09-30 19:20:33追加了内容

国庆假期将更新。

包思远在2024-10-01 21:49:47追加了内容

预计10.6日前更新!

包思远在2024-10-03 21:17:22追加了内容

在线数据存储(C++)1.0版下载地址【exe操作库+示例代码】

查看密码:syyx

示例代码如下:(和zip里的示例代码一样)

#include<bits/stdc++.h>
#include<WINSOCK2.H>
#include<windows.h>
#include<conio.h>
#define PORT           8257
#define MSGSIZE        1024
#pragma comment(lib, "ws2_32.lib")
using namespace std;
bool fileexist(string filename){
	ifstream fin;
	fin.open(filename);
	bool flag=fin.good();
	fin.close();
	return flag;
}
char IP;
WSADATA wsaData2;
SOCKET sClient2;
SOCKADDR_IN server;
bool GetLocalIP(char* ip){
	WSADATA wsaData;
	int ret=WSAStartup(MAKEWORD(2,2),&wsaData);
	if(ret!=0)return false;
	char hostname[256];
	ret=gethostname(hostname,sizeof(hostname));
	if(ret==SOCKET_ERROR)return false;
	HOSTENT* host=gethostbyname(hostname);
	if(host==NULL)return false;
	strcpy(ip,inet_ntoa(*(in_addr*)*host->h_addr_list));
	return true;
}
bool CliEnt(){
	WSAStartup(0x0202, &wsaData2);
	sClient2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	memset(&server, 0, sizeof(SOCKADDR_IN));
	server.sin_family = PF_INET;
	server.sin_port = htons(PORT);
	GetLocalIP(&IP);
	server.sin_addr.s_addr = inet_addr(&IP);
	int x=-1;
	while(x<0)x=connect(sClient2, (struct sockaddr *) &server, sizeof(SOCKADDR_IN));
	return true;
}
#define CE() CliEnt()
bool Send(string s){
	char szMessage[MSGSIZE];
	for(int i=0;i<s.size();i++){
		szMessage[i]=s[i];
	}
	int ret;
	ret=send(sClient2,szMessage,s.size(),0);
	if(ret<=0)return false;
	return true;
}
string Rece(){
	int ret;
	char szMessage[MSGSIZE];
	string s="";
	ret=recv(sClient2,szMessage,MSGSIZE,0);
	if(ret<=0)return "error";
	for(int i=0;i<ret;i++){
		s=s+szMessage[i];
	}
	return s;
}

bool C_C(){
	srand(time(0));
	GetLocalIP(&IP);
	while(!fileexist("密钥传输.txt"))Sleep(100);
	string s,t;
	ifstream fin;
	fin.open("密钥传输.txt");
	fin>>t;
	fin.close();
	char* p=&IP;
	while(*p!='\0'){
		s+=*p;
		p++;
	}
	while(1){
		bool qk=CE();
		string s1,t1;
		while((s1=Rece())=="error");
		while((t1=Rece())=="error");
		if(s==s1&&t==t1){
			Send("YES");
			Sleep(1000);
			break;
		}
		else{
			Send("NO");
		}
		closesocket(sClient2);
		WSACleanup();
	}
	return true;
}
void C_D(){
	closesocket(sClient2);
	WSACleanup();
}
string RandString(size_t size) {
	string ret;
	for (size_t i = 0; i < size; i++) {
		ret += rand() % 26 + 'a';
	}
	return ret;
}
string createoltext(string text){
	string key=RandString(30);
	string ans="1 syolcp-"+key+" ---"+text;
	Send(ans);
	while((ans=Rece())=="error")continue;
	if(ans=="1")return key;
	return "error";
}
bool deleteoltext(string key){
	string ans="2 syolcp-"+key;
	Send(ans);
	while((ans=Rece())=="error")continue;
	return ans=="1";
}
string getoltext(string key){
	string ans="3 syolcp-"+key;
	Send(ans);
	while((ans=Rece())=="error")continue;
	if(ans.substr(0,3)!="---")return "error";
	return ans.substr(3,ans.size()-3);
}
void oltext_init(){
	ShellExecute(NULL, "open", "数据在线存储.exe", NULL, NULL, SW_HIDE);
	C_C();
}
void oltext_close(){
	Send("4");
	Sleep(1000);
	C_D();
}
int main(){
	oltext_init();
	string key=createoltext("在线数据存储");
	cout<<getoltext(key)<<endl;
	deleteoltext(key);
	cout<<getoltext(key);
	oltext_close();
	return 0;
}

注:主函数是示范操作的,可根据自己的需求修改!

示例代码编译方法:

1.系统及编译器要求:

系统(要求):Windows 7及以上

编译器(推荐):

编译器配置(推荐):

2.编译参数设置:

工具->编译器选项->编译器配置->编译器->“编译时加入以下参数:”,

先在前面的框里打√,再在大框里加入:

-lws2_32

效果图:

之后大家就可以编译运行了,有什么问题可在帖子下方提出!

包思远在2024-10-03 21:20:24追加了内容

请下载zip,解压后再编译运行!

包思远在2024-10-13 14:25:40追加了内容

有时间我会发一下使用方法


0
0
石峻帆
石峻帆
中级守护
中级守护

你把你自己写的头文件发一下不就是了吗?是不是怕抄袭?

0
0
0
0
0
0
0
0
0
王铄
王铄
高级守护
高级守护

看上去好厉害,根本看不懂

(疑惑)

0
毕博雨
毕博雨
高级光能
高级光能

有点不懂

但觉得很厉害

0
包思远
包思远
新手启示者
新手启示者

看不懂也不影响,会用就行了

 

 

                                                                                       (华丽的分界线)                                                                                     

 

 

@张百川   @张百川    你看看( @王海成   )

0
0
0
0
我要回答