curl 实现 url连接 post Json消息

1..h文件#pragma once
#include <stdio.h>
#include <curl/curl.h>
#include <string>

#pragma comment(lib,"libcurl_imp.lib")

#undef DISABLE_SSH_AGENT

bool        PostMessage_Url        (const std::string & strUrl, const std::string & strPost, std::string & strResponse, tstring &strError, int &iError);

bool        GetMessage_Url        (const std::string & strUrl, std::string & strResponse, tstring &strError, int &iError);
void        GetErrorInfo        (int resId, std::wstring & strError);

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);  
	printf("pData [ %s ] \n", str);
	return nmemb;  
}  

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);  
	}  
	return 0;  
} 


//strUrl url

//strPostRequest post,json消息

//strResponse 返回消息

//strError 返回错误信息

bool CSftp::PostMessage_Url(const std::string & strUrl, const std::string & strPostRequest, std::string & strResponse, tstring &strError, int &iError)
{
	bool bError = true;
	CURLcode res;
	CURL *curl;
	struct curl_slist *headers = NULL; // init to NULL is important 
	headers = curl_slist_append( headers, "Accept: application/json");  
	headers = curl_slist_append( headers, "Content-Type: application/json");
	headers = curl_slist_append( headers, "charsets: utf-8"); 
	curl = curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
	curl_easy_setopt(curl, CURLOPT_POST, 1L);  
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPostRequest.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, 3);  
	res = curl_easy_perform(curl);  
	curl_easy_cleanup(curl);  
	if(CURLE_OK != res) {
		fprintf(stderr, "curl told us %d\n", res);
		bError = false;
		GetErrorInfo(res, strError);
		iError = res;
	}
	return bError;
}

bool CSftp::GetMessage_Url(const std::string & strUrl, std::string & strResponse, tstring &strError, int &iError)  
{  
	bool bError = true;
	CURLcode res;
	CURL *curl;
	struct curl_slist *headers = NULL; 
	headers = curl_slist_append( headers, "Accept: application/json");  
	headers = curl_slist_append( headers, "Content-Type: application/json");
	headers = curl_slist_append( headers, "charsets: utf-8"); 
	curl = curl_easy_init();
	if(NULL == curl)  
	{  
		return CURLE_FAILED_INIT;  
	}  
	if(m_bDebug)  
	{  
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
	}  
	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);  
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
	res = curl_easy_perform(curl);  
	if(CURLE_OK != res) {
		fprintf(stderr, "curl told us %d\n", res);
		bError = false;
		GetErrorInfo(res, strError);
		iError = res;
	}
	curl_easy_cleanup(curl);  

	return bError;  
}  

void CSftp::GetErrorInfo (int resId, std::wstring & strError)
{
	switch (resId)
	{
	case CURLE_COULDNT_CONNECT:
		strError = _T("couldn't connect");
		break;
	case CURLE_LOGIN_DENIED:
		strError = _T("user, password or similar was not accepted and we failed to login.");
		break;
	case CURLE_REMOTE_FILE_NOT_FOUND:
		strError = _T("remote file not found");
		break;
	case CURLE_COULDNT_RESOLVE_HOST:
		strError = _T("couldn't resolve host");
		break;
	default:
		strError = resId;
		break;
	}

}


实例下载地址:

http://download.csdn.net/detail/leftstrang/9632741


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值