求一个c++ 基于HTTPS 协议实现文件上传下载的类

weixin_39787672 2018-08-21 05:28:30
各位大神帮帮忙,感谢
...全文
377 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼上正解。libcurl。
yiyefangzhou24 2018-08-23
  • 打赏
  • 举报
回复
编译出libcurl.dll后的调用方法:

//network.h
#pragma once
#include "../curl/include/curl/curl.h"

#ifdef _DEBUG
# pragma comment(lib, "libcurl_d.lib")
#else
# pragma comment(lib, "libcurl.lib")
#endif

struct MemoryStruct {
char *memory;
size_t size;
};

class Http_request{
public:
Http_request();
~Http_request();
size_t Http_post_data(LPCSTR url, LPCSTR data);
size_t Http_file_upload();
private:
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp);
public:
struct MemoryStruct chunk;
};



//network.cpp
#include "network.h"


Http_request::Http_request(){
chunk.memory = (char *)malloc(1); /* will be grown as needed by realloc above */
chunk.size = 0; /* no data at this point */
}

Http_request::~Http_request(){
free(chunk.memory);
}

size_t Http_request::Http_post_data(LPCSTR url, LPCSTR data)
{
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {

curl_easy_setopt(curl, CURLOPT_URL, url);

/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

/* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
itself */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(data));

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%s\n", chunk.memory);
}

/* always cleanup */
curl_easy_cleanup(curl);

/* we're done with libcurl, so clean it up */
curl_global_cleanup();
}
return chunk.size;
}

size_t Http_request::Http_file_upload(){
return 0;
}

size_t Http_request::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;

mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) {
/* out of memory! */
//printf("not enough memory (realloc returned NULL)\n");
return 0;
}

memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}
半雨微凉丶 2018-08-22
  • 打赏
  • 举报
回复
winHttp
zhouqunhai 2018-08-22
  • 打赏
  • 举报
回复
libcurl应该可以
只此冒泡君 2018-08-22
  • 打赏
  • 举报
回复
libevent 也可以了解一下
yiyefangzhou24 2018-08-22
  • 打赏
  • 举报
回复
libcurl
yiyefangzhou24 2018-08-22
  • 打赏
  • 举报
回复
curl,方便简单
ztenv 版主 2018-08-22
  • 打赏
  • 举报
回复
试试curl
smwhotjay 2018-08-21
  • 打赏
  • 举报
回复

说http client不就完了
sghcpt 2018-08-21
  • 打赏
  • 举报
回复
楼主,可以看看libcurl开源库满足不满足你的需求。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧