快递查询api接口调用例子

利用c语言与快递dpi制作基于代码的查快递的小程序

调试接口 com=快递全称,不知道用auto
nu=快递单号
receiverPhone=手机后四位

例子[42-44行]

GET /showapi_expInfo?com=auto&nu=YT4052529821846&receiverPhone=9296 HTTP/1.1\r\n

编译方式:其中路径改为自己的cjson目录即可

gcc kudi.c -o kudi -I /home/lstwwa/lib/cJSON -L /home/lstwwa/lib/cJSON -lcjson -Wl,-rpath=/home/lstwwa/lib/cJSON 

AppCode失效后需要自行购买

AppCode获取网站
https://market.aliyun.com/products/57126001/cmapi010996.html?spm=5176.2020520132.101.8.71c37218CY1gC6#sku=yuncode499600008
//////////////////////////////////////////////////////////////////
//
//  Author: lstwwa  
//
//  Date: 2019-9
//
//  GitHub: github.com/lstwwa   
//
//	Bug Report:[email protected]
//
//////////////////////////////////////////////////////////////////
#include "lstwwa.h"

void remote_info(struct hostent *he)
{
	assert(he);

	printf("主机的官方名称:%s\n", he->h_name);

	int i;
	for(i=0; he->h_aliases[i]!=NULL; i++)
	{
		printf("别名[%d]:%s\n", i+1, he->h_aliases[i]);
	}

	printf("IP地址长度:%d\n", he->h_length);

	for(i=0; he->h_addr_list[i]!=NULL; i++)
	{
		printf("IP地址[%d]:%s\n", i+1,
			inet_ntoa(*((struct in_addr **)he->h_addr_list)[i]));
	}
}


void http_request(char*buf, int size)
{
	assert(buf);

	// 构建一条 HTPP 请求报文
	bzero(buf, size);
	snprintf(buf, size, "GET /showapi_expInfo?com=auto&nu=YT4052529821846&receiverPhone=9296 HTTP/1.1\r\n"
			    "Host:ali-deliver.showapi.com\r\n"
			    "Authorization:APPCODE d3b3d23343664e83a9322056e200211d\r\n\r\n");
}




int main(int argc, char **argv)
{
	// 1,根据所选择的API获取其服务器IP
	char *host = "ali-deliver.showapi.com";
	struct hostent *he = gethostbyname(host);
	if(he == NULL)
	{
		errno = h_errno;
		perror("gethostbyname() failed");
		exit(0);
	}

#ifdef DEBUG
	remote_info(he);
#endif

	struct in_addr **addr_list = (struct in_addr **)(he->h_addr_list);

	// 准备一个TCP套接字,并且连接到服务端
	int fd = socket(AF_INET, SOCK_STREAM, 0);

	struct sockaddr_in srvaddr;
	socklen_t len = sizeof(srvaddr);
	bzero(&srvaddr, len);

	srvaddr.sin_family = AF_INET;
	srvaddr.sin_port   = htons(80);
	srvaddr.sin_addr   = *addr_list[0];


	// 连接服务端
	if(connect(fd, (struct sockaddr *)&srvaddr, len) == -1)
	{
		perror("connect() failed");
		exit(0);
	}
	else
	{
		printf("连接服务器成功\n");
	}

	// 发送HTTP请求报文
	char *sndbuf = calloc(1, 1000);
	http_request(sndbuf, 1000);   


	printf("请求报文头部:\n");
	printf("+++++++++++++++++++++++++++++++\n");
	printf("%s", sndbuf);
	printf("+++++++++++++++++++++++++++++++\n");
	printf("+++++++++++++++++++++++++++++++\n");


	// 向服务器发送 HTTP 请求报文
	int n = send(fd, sndbuf, strlen(sndbuf), 0);
	if(n == -1)
	{
		perror("send() failed");
		exit(0);
	}


	printf("[%d] bytes have been sent.\n", n);
	printf("+++++++++++++++++++++++++++++++\n");
	printf("+++++++++++++++++++++++++++++++\n");
	free(sndbuf);

	char *recvbuf = calloc(1, 4096);
	int m = 0; // 头部的大小

	// 等待接收 HTTP 响应头部(头部一定以\r\n\r\n结束)
	while(1)	
	{
		int n = recv(fd, recvbuf+m, 1, 0);

		if(n == -1)
		{
			perror("recv() failed");
			exit(0);
		}

		m += n;

		if(strstr(recvbuf, "\r\n\r\n"))
			break;
	}


	printf("响应报文头部:\n");
	printf("*******************************\n");
	printf("%s", recvbuf);
	printf("*******************************\n");
	printf("*******************************\n");


	// 分析头部信息,比如响应码、正文长度……
	char *s = "Content-Length: ";
	char *p = strstr(recvbuf, s);
	int json_len;
	if(p != NULL)
	{
		json_len = atoi(p+strlen(s));
	}
	printf("正文的长度: %d\n", json_len);


	// 接收正文(JSON数据)
	char *json = calloc(1, json_len);	

	int total = 0;
	while(json_len > 0)
	{
		int n = read(fd, json+total, json_len);
		json_len -= n;
		total += n;
	}

	printf("JSON数据:\n");
	printf("*******************************\n");
	printf("%s", json);
	printf("*******************************\n");

	return 0;
}

你可能感兴趣的:(程序设计)