libcurl

多协议文件传输库

 

 

libcurl - the multiprotocol file transfer library

libcurl is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling and more!

libcurl is highly portable, it builds and works identically on numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell NetWare, DOS and more...

libcurl is freethread-safeIPv6 compatiblefeature richwell supportedfastthoroughly documented and is already used by many known, big and successful companies.

Download

Go to the regular curl download page and get the latest curl package, or one of the specific libcurl packages listed.

API

You use libcurl with the provided C API. The curl team works hard to keep the API and ABI stable. If you prefer using libcurl from your other favorite language, chances are there's already a binding written for it.

Howto

Check out our using libcurl page for general hints and advice, the free HTTP client library comparison. or read the comparisons against libwww and WinInet.

libcurl is most probably the most portable, most powerful and most often used C-based multi-platform file transfer library on this planet - be it open source or commercial.

 

 

 

 

官网:https://curl.haxx.se/libcurl/

API:https://curl.haxx.se/libcurl/c/

 

示例:

https://curl.haxx.se/libcurl/c/example.html

 

https://curl.haxx.se/libcurl/c/libcurl-tutorial.html

 

 

 

注意:

1:自己下载源码编译;比较简单;

      参考:

https://blog.csdn.net/DaSo_CSDN/article/details/77587916

 

编译命令:debug版本

E:\OpenSource\curl-7.60.0\winbuild>nmake /f Makefile.vc mode=dll VC=14 MACHINE=x86 DEBUG=yes

 

 

2:用动态链接库,否在在VC上有一些错误,可以解决,比较麻烦;

 

 

最后写一个示例:

1:获取网页信息;

2:获取接收数据;

3:参考https.c

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
/* <DESC>
 * Simple HTTPS GET
 * </DESC>
 */
#include <stdio.h>
#include <curl/curl.h>
#include <string>


std::string UTF8_To_string(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);


wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 
memset(pwBuf, 0, nwLen * 2 + 2);


MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);


int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);


char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);


WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);


std::string retStr = pBuf;


delete[]pBuf;
delete[]pwBuf;


pBuf = NULL;
pwBuf = NULL;


return retStr;
}


size_t receive_data(void *buffer, size_t size, size_t nmemb, FILE *file)
{
std::string str = UTF8_To_string(std::string((char*)buffer));
    AfxMessageBox(CString(str.c_str()));


AfxMessageBox(CString((char*)buffer));


//return 0;

return nmemb; //这里一定要返回nmemb;如:当接收的网页内容较长时,一次无法完全获取,那么这里就要准确的返回接收数据,否则无法接收收到后续数据;


}




int main_test(void)
{
  CURL *curl;
  CURLcode res;


  curl_global_init(CURL_GLOBAL_DEFAULT);


  curl = curl_easy_init();
  if(curl)
  {


// curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/");


curl_easy_setopt(curl, CURLOPT_URL, "https://192.168.0.87:8445/api/app/testconnect");  
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
 


#ifdef SKIP_PEER_VERIFICATION
    /*
     * If you want to connect to a site who isn't using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif


#ifdef SKIP_HOSTNAME_VERIFICATION
    /*
     * If the site you're connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl will refuse to connect. You can skip
     * this check, but this will make the connection less secure.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif






{






//设置回调函数: 接收
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receive_data);
// res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);






}






    /* 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
{
#if 0
//这里不可以用curl_easy_recv接收数据,参考注释;
const int nLen = 10000;
char strRecv[nLen] = { 0 };


size_t nRecv = 0;


res = curl_easy_recv(curl, strRecv, nLen, &nRecv);


if(res == CURLE_OK)
AfxMessageBox(CString(strRecv));
#endif


}


    /* always cleanup */
    curl_easy_cleanup(curl);
  }


  curl_global_cleanup();


  return 0;
}

 

//https post:

curl_easy_setopt(curl, CURLOPT_URL, strURL.c_str());


strParam = string_to_utf8(strParam);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strParam.c_str() );


curl_easy_setopt(curl, CURLOPT_POST, 1);


res = curl_easy_perform(curl);


if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));


//CV2LibCurl::receive_data(NULL, (int)res, 0, NULL);

}

 

 

//https DELETE:

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");


curl_easy_setopt(curl, CURLOPT_URL, strURL.c_str());


curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);



 

res = curl_easy_perform(curl);

 

 

在Windows环境中,接收回调函数与curl_easy_perform函数是阻塞执行的的,所以很方便获取数值后返回结果;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值