通过libcurl向服务端发送数据的2种方式:get/post
2015-09-16 16:00
567 查看
1.get方式发送数据
url格式:服务器url地址+ 接口url地址+"?"+发送的数据(特定格式),如:
http://172.16.3.123:8080/temp/getScreenImages?userDes=4E&userKey=D7B7BA3F24A84D738D8B7A15CC30201A&dataLen=0&data=
参考代码如下:get方式下载文件[b]并传递数据[/b]
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
out->stream=fopen(out->filename, "wb");//打开文件进行写入
if(!out->stream)
return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
bool cathttpclient::get(IN const std::string &stshortrurl, IN const std::string &strshortdata,
IN const std::string &strfilepath)
{
bool bret = false;
if (stshortrurl.empty() || strfilepath.empty())
{
LOG_ALL_ERROR(QString("failed download param is empty."));
return bret;
}
std::string strpostdata("");
GetPacketInfo(strshortdata, strpostdata);
//strurl like "http://172.16.3.123:8080/temp/getScreenImages?userDes=4E&userKey=D7B7BA3F24A84D738D8B7A15CC30201A&dataLen=0&data=";
std::string strurl(m_strServerUrl + stshortrurl+"?"+strpostdata);
CURL *curl = nullptr;
CURLcode res;
struct FtpFile ftpfile={
strfilepath.c_str(),
nullptr
};
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
char errbuf[CURL_ERROR_SIZE];
set_share_handle(curl);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, sstrCookiePath.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, sstrCookiePath.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
LOG_ALL_ERROR(QString(errbuf));
}
else
{
bret = true;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
}
if(ftpfile.stream)
fclose(ftpfile.stream);
return bret;
}
2.post方式发送数据
url格式:服务器url地址+ 接口url地址
数据发送:
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strpostdata.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strpostdata.size());
[b][b]参考代码如下:post方式下载文件并传递数据
[/b][/b]
bool cathttpclient::DownloadFile(IN const std::string &stshortrurl, IN const std::string &strshortdata,
IN const std::string &strfilepath)
{
bool bret = false;
if (stshortrurl.empty() || strfilepath.empty())
{
LOG_ALL_ERROR(QString("failed download param is empty."));
return bret;
}
std::string strurl(m_strServerUrl + stshortrurl);
std::string strpostdata("");
GetPacketInfo(strshortdata, strpostdata);
CURL *curl = nullptr;
CURLcode res;
struct FtpFile ftpfile={
strfilepath.c_str(),
nullptr
};
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
char errbuf[CURL_ERROR_SIZE];
set_share_handle(curl);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strpostdata.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strpostdata.size());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, sstrCookiePath.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, sstrCookiePath.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
LOG_ALL_ERROR(QString(errbuf));
bret = res == CURLE_OK;
curl_easy_cleanup(curl);
curl_global_cleanup();
}
if(ftpfile.stream)
fclose(ftpfile.stream);
return bret;
}
url格式:服务器url地址+ 接口url地址+"?"+发送的数据(特定格式),如:
http://172.16.3.123:8080/temp/getScreenImages?userDes=4E&userKey=D7B7BA3F24A84D738D8B7A15CC30201A&dataLen=0&data=
参考代码如下:get方式下载文件[b]并传递数据[/b]
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
out->stream=fopen(out->filename, "wb");//打开文件进行写入
if(!out->stream)
return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
bool cathttpclient::get(IN const std::string &stshortrurl, IN const std::string &strshortdata,
IN const std::string &strfilepath)
{
bool bret = false;
if (stshortrurl.empty() || strfilepath.empty())
{
LOG_ALL_ERROR(QString("failed download param is empty."));
return bret;
}
std::string strpostdata("");
GetPacketInfo(strshortdata, strpostdata);
//strurl like "http://172.16.3.123:8080/temp/getScreenImages?userDes=4E&userKey=D7B7BA3F24A84D738D8B7A15CC30201A&dataLen=0&data=";
std::string strurl(m_strServerUrl + stshortrurl+"?"+strpostdata);
CURL *curl = nullptr;
CURLcode res;
struct FtpFile ftpfile={
strfilepath.c_str(),
nullptr
};
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
char errbuf[CURL_ERROR_SIZE];
set_share_handle(curl);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, sstrCookiePath.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, sstrCookiePath.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
LOG_ALL_ERROR(QString(errbuf));
}
else
{
bret = true;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
}
if(ftpfile.stream)
fclose(ftpfile.stream);
return bret;
}
2.post方式发送数据
url格式:服务器url地址+ 接口url地址
数据发送:
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strpostdata.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strpostdata.size());
[b][b]参考代码如下:post方式下载文件并传递数据
[/b][/b]
bool cathttpclient::DownloadFile(IN const std::string &stshortrurl, IN const std::string &strshortdata,
IN const std::string &strfilepath)
{
bool bret = false;
if (stshortrurl.empty() || strfilepath.empty())
{
LOG_ALL_ERROR(QString("failed download param is empty."));
return bret;
}
std::string strurl(m_strServerUrl + stshortrurl);
std::string strpostdata("");
GetPacketInfo(strshortdata, strpostdata);
CURL *curl = nullptr;
CURLcode res;
struct FtpFile ftpfile={
strfilepath.c_str(),
nullptr
};
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
char errbuf[CURL_ERROR_SIZE];
set_share_handle(curl);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strpostdata.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strpostdata.size());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, sstrCookiePath.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, sstrCookiePath.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
LOG_ALL_ERROR(QString(errbuf));
bret = res == CURLE_OK;
curl_easy_cleanup(curl);
curl_global_cleanup();
}
if(ftpfile.stream)
fclose(ftpfile.stream);
return bret;
}
相关文章推荐
- curl和libcurl的区别简介
- 如何使用libcurl实现HTTP的GET方法获取文件长度
- Curl使用(一)
- 使用libcurl 支持https
- 使用libcurl库编写HTTP客户端(包括GET/POST/HTTPS)
- libcurl 通过http协议下载文件并显示下载进度
- libcurl 上传文件至 web服务器
- libcurl 示例代码
- Linux 下libcurl简明教程
- Libcurl的初步实现tfp上传下载功能
- libcurl 使用的几个注意事项
- C++开源库之Curl
- linux编译curl库的动态库so
- libcurl使用与例程
- 将Cocos2d-x的libcurl单独打包到Android
- Cocos2d-x中libcurl库的使用(1)查看库的版本信息
- Cocos2d-x中libcurl库的使用(2)curl_easy API的介绍
- 使用libcurl第三方库实现Android异步任务
- Cocos2d-x中libcurl库的使用(4)进度显示
- Cocos2d-x中libcurl库的使用(5)代理介绍