如何实现客户端https post.

Freeze_Z 2011-01-24 06:07:02
如何实现客户端https post.

基于openssl库或者curl库都可以.

知道curl库实现啦,可是不知道如何调用接口.
...全文
564 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
williom90320 2013-08-13
  • 打赏
  • 举报
回复
引用 6 楼 jaylong35 的回复:


/*------------------------------load_date--------------------------------------------
*
*func:receive the date from the server and save it to the userp
*@param userp :the buffer used to save the date receive from server
*return the size of the string receive
--------------------------------------------------------------------------------------*/
size_t load_date( void *buffer, size_t size, size_t nmemb, char *userp ) 
{ //处理接收到数据的回调函数,其中buffer就是	curl_easy_setopt(curl,CURLOPT_WRITEDATA,
  //wr_buf); 这句传进来的参数wr_buf用来保存数据

	int  wr_index=0; 
	size_t segsize = size * nmemb; 
	if ( wr_index + segsize > MAX_BUF ) { 
		userp[0] = 0; 
		return 0; 
	} 

	/* copy the data from the curl buffer into our buffer */ 
	memcpy( static_cast<void *>(&userp[wr_index]), buffer, segsize ); 

	/* update the write index */ 
	wr_index += static_cast<int>(segsize); 

	/* null terminate the buffer */ 
	userp[wr_index] = 0;
	printf("%s",userp);
	/* return the number of bytes received, indicating to curl that all is okay */ 
	return segsize; 
} 

/*------------------------------load_header--------------------------------------------
*
*func:receive the header date from the server and save it to the file stream
*@param stream :the temp file used to save the header date receive from the server
*return the size of the string receive
--------------------------------------------------------------------------------------*/
size_t load_header( void *buffer, size_t size, size_t nmemb, FILE *stream ) 
{ //这个是处理接收到的HTTP头的处理回调函数我这里是把头写到文件当中

	return fwrite(buffer,size,nmemb,stream);
} 

/*------------------------------send_https--------------------------------------------
*
*@func:encapsulate the package and send it to the server 
*@param url :the server address
*@param http_headers :the header of the request
*@param poststring :the post parameter of the request
*@param timeout :the timeout
*@return the error code
--------------------------------------------------------------------------------------*/
int send_https(const char *url, curl_slist *http_headers, const char *poststring,unsigned long timeout,char * wr_buf)
{
	CURLcode ret;
	int error_code=SUCEED_OK;
	static const char *headerfilename = "head.out";  
	FILE *headerfile;
	headerfile = fopen(headerfilename,"wb");
	if (headerfile == NULL) {
		return -1;
	}

	if(!curl)  return ERROR_CURL_INITIALFALIED;
	//调置HTTP请求包的头,这里当参数传入 
	curl_easy_setopt(curl,CURLOPT_HTTPHEADER,http_headers);
	//开启OPENSSL通道,允许接收HTTPS协议 
 	curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);
	curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);	
	//设置要访问的URL 
	curl_easy_setopt(curl,CURLOPT_URL, url);
	//设置要post的数据 
	curl_easy_setopt(curl,CURLOPT_POSTFIELDS,poststring);
    //设置连接超时	
    curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,timeout);
    //设置数据接收缓冲区,这个要看回调函数处理 
	curl_easy_setopt(curl,CURLOPT_WRITEDATA, wr_buf); 
    //设置数据接收回调函数 
	curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,load_date); 
	//设置HTTP头数据处理回调函数 
	curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,load_header);
	//设置HTTP头数据接收缓冲,这里是文件 
	curl_easy_setopt(curl,CURLOPT_WRITEHEADER,headerfile);
	ret = curl_easy_perform(curl);
    if ( ret == 0 ) 
	{
		int error_code=SUCEED_OK;
	}
	else 
	{
		switch(ret)
		{
		case CURLE_COULDNT_CONNECT:
			{
				printf("ERROR:TimeOut can't connect to the host.\n");
				error_code=ERROR_CURL_TIMEOUT;
				break;
			}
		case CURLE_HTTP_RETURNED_ERROR:
			{
				printf("ERROR:HTTP return false.");
				error_code=ERROR_CURL_HTTPFALSE;
				break;
			}
		case CURLE_SSL_ENGINE_INITFAILED:
			{
				printf("ERROR:SSL can't be initialized.");
				error_code=ERROR_CURL_SSLINITFAILED;
				break;
			}
		case CURLE_COULDNT_RESOLVE_HOST:
			{
				printf("ERROR:SSL can't create ssl connection.");
				error_code=ERROR_CURL_SSLINITFAILED;
				break;
			}
		default:
			{
				printf("ERROR:correspond failed.");
				error_code=ERROR_FAILED_SEND;
			}
		}
	}
	fclose(headerfile);
	if(error_code == SUCEED_OK)
		error_code =parase_rev(wr_buf);//这一句是处理接收到的数据 
	strCheckSum = getChecksum(strUserID.c_str(),strPID.c_str(),strFirmStr.c_str());	
	
	return error_code;
}
int main()
{
    char wr_buf[65530];
    //初始化HTTP请求头 
	curl_slist *headers=NULL; /* init to NULL is important */
    headers = curl_slist_append(headers, "Accept-Language: zh-cn");
	headers = curl_slist_append(headers, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
	headers = curl_slist_append(headers, "Content-Type: text/xml");
	headers = curl_slist_append(headers, "Connection:");
	send_https("www.baidu.com",headers,"eeeeeeeeeee",1000,wr_buf);
}
这只是把原来我写过的一个程序进行节选 其中一些返回码如ERROR_FAILED_SEND这些,是要自己定义的
可以把完整的代码发我么?谢谢
jaylong35 2011-01-27
  • 打赏
  • 举报
回复
解决了就好,不客气
Freeze_Z 2011-01-27
  • 打赏
  • 举报
回复
谢谢.
换过你的接口调用. 已经OK啦.
问题好像出在之前编译的curl库没有支持到openssl.

多谢你...



jaylong35 2011-01-26
  • 打赏
  • 举报
回复
对了,原来这个是一个类的函数,所以curl初始化在别的地方处理了,这个函数里没有,你自己要重新处理一下
jaylong35 2011-01-26
  • 打赏
  • 举报
回复


/*------------------------------load_date--------------------------------------------
*
*func:receive the date from the server and save it to the userp
*@param userp :the buffer used to save the date receive from server
*return the size of the string receive
--------------------------------------------------------------------------------------*/
size_t load_date( void *buffer, size_t size, size_t nmemb, char *userp )
{ //处理接收到数据的回调函数,其中buffer就是 curl_easy_setopt(curl,CURLOPT_WRITEDATA,
//wr_buf); 这句传进来的参数wr_buf用来保存数据

int wr_index=0;
size_t segsize = size * nmemb;
if ( wr_index + segsize > MAX_BUF ) {
userp[0] = 0;
return 0;
}

/* copy the data from the curl buffer into our buffer */
memcpy( static_cast<void *>(&userp[wr_index]), buffer, segsize );

/* update the write index */
wr_index += static_cast<int>(segsize);

/* null terminate the buffer */
userp[wr_index] = 0;
printf("%s",userp);
/* return the number of bytes received, indicating to curl that all is okay */
return segsize;
}

/*------------------------------load_header--------------------------------------------
*
*func:receive the header date from the server and save it to the file stream
*@param stream :the temp file used to save the header date receive from the server
*return the size of the string receive
--------------------------------------------------------------------------------------*/
size_t load_header( void *buffer, size_t size, size_t nmemb, FILE *stream )
{ //这个是处理接收到的HTTP头的处理回调函数我这里是把头写到文件当中

return fwrite(buffer,size,nmemb,stream);
}

/*------------------------------send_https--------------------------------------------
*
*@func:encapsulate the package and send it to the server
*@param url :the server address
*@param http_headers :the header of the request
*@param poststring :the post parameter of the request
*@param timeout :the timeout
*@return the error code
--------------------------------------------------------------------------------------*/
int send_https(const char *url, curl_slist *http_headers, const char *poststring,unsigned long timeout,char * wr_buf)
{
CURLcode ret;
int error_code=SUCEED_OK;
static const char *headerfilename = "head.out";
FILE *headerfile;
headerfile = fopen(headerfilename,"wb");
if (headerfile == NULL) {
return -1;
}

if(!curl) return ERROR_CURL_INITIALFALIED;
//调置HTTP请求包的头,这里当参数传入
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,http_headers);
//开启OPENSSL通道,允许接收HTTPS协议
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);
//设置要访问的URL
curl_easy_setopt(curl,CURLOPT_URL, url);
//设置要post的数据
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,poststring);
//设置连接超时
curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,timeout);
//设置数据接收缓冲区,这个要看回调函数处理
curl_easy_setopt(curl,CURLOPT_WRITEDATA, wr_buf);
//设置数据接收回调函数
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,load_date);
//设置HTTP头数据处理回调函数
curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,load_header);
//设置HTTP头数据接收缓冲,这里是文件
curl_easy_setopt(curl,CURLOPT_WRITEHEADER,headerfile);
ret = curl_easy_perform(curl);
if ( ret == 0 )
{
int error_code=SUCEED_OK;
}
else
{
switch(ret)
{
case CURLE_COULDNT_CONNECT:
{
printf("ERROR:TimeOut can't connect to the host.\n");
error_code=ERROR_CURL_TIMEOUT;
break;
}
case CURLE_HTTP_RETURNED_ERROR:
{
printf("ERROR:HTTP return false.");
error_code=ERROR_CURL_HTTPFALSE;
break;
}
case CURLE_SSL_ENGINE_INITFAILED:
{
printf("ERROR:SSL can't be initialized.");
error_code=ERROR_CURL_SSLINITFAILED;
break;
}
case CURLE_COULDNT_RESOLVE_HOST:
{
printf("ERROR:SSL can't create ssl connection.");
error_code=ERROR_CURL_SSLINITFAILED;
break;
}
default:
{
printf("ERROR:correspond failed.");
error_code=ERROR_FAILED_SEND;
}
}
}
fclose(headerfile);
if(error_code == SUCEED_OK)
error_code =parase_rev(wr_buf);//这一句是处理接收到的数据
strCheckSum = getChecksum(strUserID.c_str(),strPID.c_str(),strFirmStr.c_str());

return error_code;
}
int main()
{
char wr_buf[65530];
//初始化HTTP请求头
curl_slist *headers=NULL; /* init to NULL is important */
headers = curl_slist_append(headers, "Accept-Language: zh-cn");
headers = curl_slist_append(headers, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
headers = curl_slist_append(headers, "Content-Type: text/xml");
headers = curl_slist_append(headers, "Connection:");
send_https("www.baidu.com",headers,"eeeeeeeeeee",1000,wr_buf);
}

这只是把原来我写过的一个程序进行节选
其中一些返回码如ERROR_FAILED_SEND这些,是要自己定义的
Freeze_Z 2011-01-26
  • 打赏
  • 举报
回复
是不是发错地方啦.

淫才这么多的,帮忙解答一下.
Freeze_Z 2011-01-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yui 的回复:]
curl的官方网站就带有不少例子

http://curl.haxx.se/docs/
[/Quote]

例程中有http post的和https get的.
修改下,好像仍然不通.

curl实现啦,可是不知道怎么调用接口.
以下是我乱修改的,不通.


curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);

curl_easy_setopt(curl, CURLOPT_POST, 1 );

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

res = curl_easy_perform(curl);

curl_easy_cleanup(curl);
}






yui 2011-01-24
  • 打赏
  • 举报
回复
curl的官方网站就带有不少例子

http://curl.haxx.se/docs/

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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