Curl 同步请求

 

同步请求相对较简单,代码示例如下:

//HttpClient.h  头文件

#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__

#include <string>

class CHttpClient
{
public:
	static CHttpClient& GetInstance();

public:
	CHttpClient(void);
	~CHttpClient(void);

	/**
	* @brief HTTP POST请求
	* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
	* @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
	* @param strResponse 输出参数,返回的内容
	* @return 返回是否Post成功
	*/
    int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char* szHost = NULL);

	/**
	* @brief HTTP GET请求
	* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
	* @param strResponse 输出参数,返回的内容
	* @return 返回是否Post成功
	*/
    int Get(const std::string & strUrl, std::string & strResponse, const char* szHost = NULL);

	/**
	* @brief HTTPS POST请求,无证书版本
	* @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
	* @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
	* @param strResponse 输出参数,返回的内容
	* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
	* @return 返回是否Post成功
	*/
    int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char* szHost = NULL, const char * pCaPath = NULL);

	/**
	* @brief HTTPS GET请求,无证书版本
	* @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
	* @param strResponse 输出参数,返回的内容
	* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
	* @return 返回是否Post成功
	*/
    int Gets(const std::string & strUrl, std::string & strResponse, const char* szHost = NULL, const char * pCaPath = NULL);

	const char* GetErrorStr(const int& rknErrorCode);

};

#endif

 

//HttpClient.cpp 

#include "HttpClient.h"
#include "curl/curl.h"
#include <string>

#define TIME_OUT 10

CHttpClient& CHttpClient::GetInstance()
{
	static CHttpClient s_iHttpClient;
	return s_iHttpClient;
}

CHttpClient::CHttpClient(void)
{
	curl_global_init(CURL_GLOBAL_ALL);
}

CHttpClient::~CHttpClient(void)
{
	curl_global_cleanup();
}

static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
	if(itype == CURLINFO_TEXT)
	{
		//printf("[TEXT]%s\n", pData);
	}
	else if(itype == CURLINFO_HEADER_IN)
	{
		printf("[HEADER_IN]%s\n", pData);
	}
	else if(itype == CURLINFO_HEADER_OUT)
	{
		printf("[HEADER_OUT]%s\n", pData);
	}
	else if(itype == CURLINFO_DATA_IN)
	{
		printf("[DATA_IN]%s\n", pData);
	}
	else if(itype == CURLINFO_DATA_OUT)
	{
		printf("[DATA_OUT]%s\n", pData);
	}
	else if (NULL != pData)
	{
		printf("[UNKNOW]%s\n", pData);
	}
	return 0;
}

static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
	std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
	if(NULL == str || NULL == buffer)
	{
		return -1;
	}

	char* pData = (char*)buffer;
	str->append(pData, size * nmemb);
	return nmemb;
}

int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char* szHost)
{
	CURLcode res;
	CURL* curl = curl_easy_init();
	if(NULL == curl)
	{
		return CURLE_FAILED_INIT;
	}
#define _DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_POST, 1);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT);

	struct curl_slist *headers = NULL;
	if (NULL != szHost)
	{
		headers = curl_slist_append(headers, szHost);
		res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	}
	res = curl_easy_perform(curl);
	if (NULL != szHost)
	{
		curl_slist_free_all(headers);
	}

	curl_easy_cleanup(curl);
	return res;
}

int CHttpClient::PostByHttpHead(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
{
    CURLcode res;
    CURL* curl = curl_easy_init();
    if (NULL == curl)
    {
        return CURLE_FAILED_INIT;
    }
#define _DEBUG
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    return res;
}

int CHttpClient::Get(const std::string & strUrl, std::string & strResponse, const char* szHost)
{
	CURLcode res;
	CURL* curl = curl_easy_init();
	if(NULL == curl)
	{
		return CURLE_FAILED_INIT;
	}
#define _DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
	/**
	* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
	* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
	*/
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT);
    struct curl_slist *headers = NULL;
    if (NULL != szHost)
    {
        headers = curl_slist_append(headers, szHost);
        res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    }
	res = curl_easy_perform(curl);
    if (NULL != szHost)
    {
        curl_slist_free_all(headers);
    }
	curl_easy_cleanup(curl);
	return res;
}

int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char* szHost, const char * pCaPath)
{
	CURLcode res;
	CURL* curl = curl_easy_init();
	if(NULL == curl)
	{
		return CURLE_FAILED_INIT;
	}
#define _DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_POST, 1);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	if(NULL == pCaPath)
	{
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
	}
	else
	{
		//缺省情况就是PEM,所以无需设置,另外支持DER
		//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
		curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
	}
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT);

	struct curl_slist *headers = NULL;
	if (NULL != szHost)
	{
		headers = curl_slist_append(headers, szHost);
		res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	}
	res = curl_easy_perform(curl);
	if (NULL != szHost)
	{
		curl_slist_free_all(headers);
	}

	curl_easy_cleanup(curl);
	return res;
}

int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char* szHost, const char * pCaPath)
{
	CURLcode res;
	CURL* curl = curl_easy_init();
	if(NULL == curl)
	{
		return CURLE_FAILED_INIT;
	}
#define _DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	if(NULL == pCaPath)
	{
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
	}
	else
	{
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
		curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
	}
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT);

	struct curl_slist *headers = NULL;
	if (NULL != szHost)
	{
		headers = curl_slist_append(headers, szHost);
		res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	}
	res = curl_easy_perform(curl);
	if (NULL != szHost)
	{
		curl_slist_free_all(headers);
	}

	curl_easy_cleanup(curl);
	return res;
}

const char* CHttpClient::GetErrorStr(const int& rknErrorCode)
{
	return curl_easy_strerror(CURLcode(rknErrorCode));
}

调用示例:

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include "HttpClient.h"

int main()
{
    std::string  strResponse;
    CHttpClient::GetInstance()->Get("www.baidu.com",strResponse);
    printf("strResponse = %s\n", strResponse.c_str());
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
<?php class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_POST, 1); if($post_data != ''){ curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); } curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HEADER, false); $file_contents = curl_exec($ch); curl_close($ch);Post()函数用于向窗口发送非PowerBuilder预定义事件的消息,这个窗口可以是PowerBuilder应用的窗口,也可以是其它应用的窗口。Post()函数把发送的消息放置在指定窗口消息队列的尾部,然后返回到应用程序中,它并不等待相应事件事件处理程序的执行。这一点与Send()函数不同,Send()函数直接触发指定窗口相应的事件,执行事件处理程序后返回到调用应用中。因此,我们说Post()函数采用的是异步方式,Send()函数采用的是同步方式。Post()函数的参数handle指定接收消息的窗口句柄,对PowerBuilder窗口来说,使用Handle()函数可以得到该句柄。对其它应用程序的窗口来说,可以调用系统API函数查找窗口并得到相应窗口的句柄。如果应用程序要邮寄PowerBuilder定义事件(包括预定义事件和用户定义事件),那么使用PostEvent()函数既简单有方便。当应用程序在long参数位置指定一个字符串时,Post()函数复制一个该字符串的副本,然后将副本的地址传送给指定窗口。
### 回答1: C语言的CURL库是常用的网络传输工具,它可以发送HTTP请求和接收服务器响应。在使用CURL进行POST请求时,可以通过设置回调函数来利用多线程实现并发请求。通过这种方式,可以大大提高程序的效率和响应速度。 具体实现方式可以通过以下步骤: 1. 定义一个包含POST参数的结构体,并为每个线程设置不同的结构体。 2. 在每个线程中,使用CURLeasy_init()函数初始化一个easy_handle,并使用curl_easy_setopt()函数设置POST参数、请求地址等选项。 3. 设置一个回调函数,用于处理从服务器返回的数据,并将处理结果存储起来。 4. 开始并发请求,每个线程分别执行curl_easy_perform()函数发送请求,并等待服务器响应。 5. 所有请求完成后,处理所有线程收集到的数据。 需要注意的是,如果并发请求的数量太大,服务器可能会出现过载的情况,需要合理控制线程数和请求数。 除此之外,还需要注意线程安全的问题。由于多个线程会同时操作同一个easy_handle,容易出现竞争条件和数据不一致的情况。需要通过线程锁等机制来解决这些问题,保证程序的正确性。 总之,通过利用CURL库和多线程技术,可以实现高效的并发POST请求。这对于需要频繁向服务器发送请求的程序来说,是非常有意义的优化。 ### 回答2: cURL是一个强大的命令行工具,可以用来发送HTTP请求。它支持多种协议,包括HTTP、HTTPS、FTP等。 使用cURL进行post并发请求,可以通过以下步骤实现: 1. 创建一个cURL句柄,设置请求的URL,请求方法为POST,并设置POST数据。 2. 将cURL句柄加入到multi_handle中,使用curl_multi_add_handle函数。 3. 使用curl_multi_perform函数启动请求。 4. 使用curl_multi_info_read函数读取请求的返回信息,判断请求是否完成。 5. 如果请求未完成,则继续调用curl_multi_perform函数。 6. 如果请求已完成,则使用curl_multi_remove_handle函数将cURL句柄从multi_handle中删除。 7. 释放cURL句柄和multi_handle。 在并发请求中,可以设置多个cURL句柄,将它们加入到multi_handle中,同时启动多个请求。这样可以提高请求的效率。 需要注意的是,使用cURL进行并发请求时,必须设置超时时间,以避免请求长时间卡在某个请求上,导致整个请求堵塞。 总的来说,使用cURL进行post并发请求,可以有效提高请求效率,减少请求时间。但同时也需要注意并发数的控制,避免产生过多的网络请求,影响系统稳定性。 ### 回答3: curl是一种命令行工具和一个库,可以用于发送和接收HTTP请求,支持各种协议和数据传输方式。使用curl可以简单快速地进行HTTP请求,并发请求则可以提高请求效率。 在C语言中,可以使用curl库来进行HTTP请求,使用curl_easy_setopt函数设置请求选项,然后使用curl_easy_perform函数执行请求。要进行并发请求,可以使用curl_multi_init函数初始化multi curl对象,然后使用curl_multi_add_handle函数将多个curl_easy句柄添加到multi curl对象中。最后使用curl_multi_perform函数执行并发请求。 在进行curl并发请求时需要注意以下几点: - 需要使用multi curl对象来进行并发请求,单个curl_easy句柄不支持并发请求。 - 在设置请求选项时,需要使用curl_easy_setopt设置多个选项,如CURLOPT_URL、CURLOPT_POSTCURLOPT_POSTFIELDS等。 - 在执行并发请求时,需要使用curl_multi_perform函数。此函数会异步执行所有添加到multi curl对象中的请求,并等待所有请求完成。 - 可以使用curl_multi_info_read函数来获取已完成的请求信息,如响应状态码、响应头信息等。 需要注意的是,并发请求可能会产生并发问题,如资源竞争、内存泄漏等。因此,需要仔细设计并发请求的逻辑,处理好多线程之间的同步和互斥问题,以保证程序的稳定性和效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二丶九

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

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

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

打赏作者

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

抵扣说明:

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

余额充值