利用聚合API平台的API接口,利用HTTP协议向服务器发送请求,并接受服务器的响应,要求利用cJSON库对服务器的响应数据进行解析,并输出到终端

题目


利用聚合API平台的API接口,利用HTTP协议向服务器发送请求,并接受服务器的响应,要求利用cJSON库对服务器的响应数据进行解析,并输出到终端

分析


1.需从代码托管网站GitHub或SourceForge代码网站下载cJSON库及阅读下载的README相关手册如何使用cJSON库;

2.使用聚合API平台的笑话大全的API,URL需使用自己的接口密钥;

3.服务器响应回来的包体,需使用strstr()函数查找子串,找到JSON格式的字符串。

代码


/***********************************************************************************
*
*	file name:	demo.c
*	author	 :  cnzycwp@126.com 
*	date	 :  2024/06/11
*	function :  该案例是利用聚合API平台的API接口,利用HTTP协议向服务器发送请求,并接受
*               服务器的响应,要求利用cJSON库对服务器的响应数据进行解析,并输出到终端
* 	note	 :  该函数使用了cJSON库,需要先下载cJSON库,并将其包含到工程中
*               编译时需要添加cJSON.c文件,即gcc demo.c cJSON.c -o demo
*   version  :
*
*	CopyRight (c)  2023-2024   cnzycwp@126.com   All Right Reseverd 
*
* **********************************************************************************/
/************************************头文件*****************************************/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "cJSON.h"
/***********************************************************************************/
/************************************宏定义*****************************************/
#define  PORT  	80
#define  IPADDR "203.107.54.210"
#define  APPKEY "xxx"  //聚合API平台的API接口密钥
/***********************************************************************************/
int main(int argc, char const *argv[])
{
	//1.创建TCP套接字
	int tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (tcp_socket == -1)
	{
		fprintf(stderr, "tcp socket error,errno:%d,%s\n",errno,strerror(errno));
		exit(1);
	}

	
	//2.发起连接请求,等待接受服务器接受连接
	struct sockaddr_in  dest_addr;
	dest_addr.sin_family 		= AF_INET; 						//协议族,是固定的
	dest_addr.sin_port   		= htons(PORT);					//服务器端口,必须转换为网络字节序
	dest_addr.sin_addr.s_addr   = inet_addr(IPADDR);			//服务器地址   


	int ret = connect(tcp_socket,(struct sockaddr *)&dest_addr,sizeof(dest_addr));
	if (ret < 0)
	{
		fprintf(stderr, "connect error,errno:%d,%s\n",errno,strerror(errno));
		exit(1);
	}


	//3.用于存储HTTP的请求内容: 请求行 + 请求字段 + \r\n + 请求包体(可选)  
	char reqbuf[1024] = {0};

	sprintf(reqbuf,"GET http://v.juhe.cn/joke/content/list.php?key=%s&sort=desc&page=1&pagesize=1&time=1418816972 "
				   "HTTP/1.1"
				   "\r\n"
				   "Host:v.juhe.cn\r\n"
                   "Content-Type:application/x-www-form-urlencoded\r\n"
				   "\r\n"
		    ,APPKEY);


	//4.说明双方建立连接,此时可以利用HTTP协议发送请求信息,并等待服务器的响应  基于请求/响应
	send(tcp_socket,reqbuf,strlen(reqbuf),0);

	//5.等待服务器的响应
	char recvbuf[1024] = {0};
	
	recv(tcp_socket,recvbuf,sizeof(recvbuf),0);  //第一次返回的响应参数

    //6.查找子串
    char *recv_body = strstr(recvbuf,"{");
	
	//7.对响应包体进行JSON解析
	
	//1) 先把获取的字符串转换为JSON格式
	cJSON * obj = cJSON_Parse(recv_body);

	//2) 把解析之后的JSON格式进行输出,用于调试
	// printf("%s\n",cJSON_Print(obj));

	//3) 对JSON格式进行解析
    cJSON * result = NULL;
	result = cJSON_GetObjectItem(obj, "result");

    cJSON * data = NULL;
	data = cJSON_GetObjectItem(result, "data");

    cJSON * obj1 = NULL;
    obj1 = cJSON_GetArrayItem(data, 0);

    cJSON * content = NULL;
    content = cJSON_GetObjectItem(obj1, "content");

    //8.显示笑话内容
    printf("content : %s\n",content->valuestring);

	//9.关闭套接字
    close(tcp_socket);

	return 0;
}

结果


image

posted @ 2024-06-11 21:24  陳文鹏  阅读(16)  评论(0编辑  收藏  举报