找回密码
 立即注册
首页 业界区 科技 HTTP-获取星座运势

HTTP-获取星座运势

渭茱瀑 昨天 10:10
使用http服务向聚合API发送请求获取运势的请求,并对接收到的数据进行JSON解析
  1. /**************************************************************************
  2. *
  3. * 设计http程序,客户端向聚合API发送获取星座运势的请求,并解析出收到数据,使用JSON进行解析
  4. * author:jindouliu2024@163.com
  5. * date:2025.5.22
  6. *
  7. *                   
  8. *
  9. * Copyright (c)  2024-2025   jindouliu2024@163.com   All right Reserved
  10. * *************************************************************************/
  11. #include<stdio.h>
  12. #include<time.h>
  13. #include <unistd.h>
  14. #include<string.h>
  15. #include<errno.h>
  16. #include<stdlib.h>
  17. #include
  18. #include <unistd.h>
  19. #include<netinet/udp.h>
  20. #include<sys/socket.h>
  21. #include<netinet/in.h>
  22. #include <pthread.h>
  23. #include <netdb.h>
  24. #include "cJSON.h"
  25. #define PORT  80
  26. #define DNS  "web.juhe.cn"
  27. #define KEY "   "//自己从聚合API申请的密钥
  28. #define TYPE "week"
  29. #define CONSNAME "%E5%8F%8C%E9%B1%BC%E5%BA%A7"//双鱼座,可以换成自己的星座
  30. int main(int argc, char *argv[]) {
  31.     char buf[1024] = {0};
  32.     char rcvbuf[1024] = {0};
  33.     char *response = malloc(4096);
  34.     if (response == NULL) {
  35.         fprintf(stderr, "Memory allocation failed\n");
  36.         return 1;
  37.     }
  38.     bzero(response, 4096);
  39.     struct hostent *host;
  40.     // 创建套接字文件
  41.     int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  42.     if(socket_fd == -1){
  43.         fprintf(stderr,"socket error errno:%d,%s",errno,strerror(errno));
  44.         return 1;
  45.     }
  46.     host = gethostbyname(DNS);
  47.     if(host == NULL){
  48.         fprintf(stderr,"gethostbyname error errno:%d,%s",errno,strerror(errno));
  49.         return 2;
  50.     }
  51.     // 绑定服务器的端口和地址
  52.     struct sockaddr_in server;
  53.     server.sin_family = AF_INET;
  54.     server.sin_port = htons(PORT);
  55.     server.sin_addr.s_addr =  ((struct in_addr *)(host->h_addr_list[0]))->s_addr;
  56.    
  57.     // 申请连接
  58.     int flag = connect(socket_fd,(struct sockaddr *)&server,sizeof(server));
  59.     if(flag == -1){
  60.         fprintf(stderr,"connect error errno:%d,%s",errno,strerror(errno));
  61.         return 1;
  62.     }
  63.    
  64.     // HTTP协议格式
  65.     sprintf(buf,"GET /constellation/getAll?key=%s&consName=%s&type=%s HTTP/1.1\r\n"
  66.             "Host: web.juhe.cn\r\n"
  67.             "Connection: close\r\n"
  68.             "\r\n", KEY, CONSNAME, TYPE);
  69.     // 发送请求
  70.     send(socket_fd, buf, strlen(buf), 0);
  71.     // 接收响应数据
  72.     int bytes_received;
  73.     while ((bytes_received = recv(socket_fd, rcvbuf, sizeof(rcvbuf) - 1, 0)) > 0) {
  74.         rcvbuf[bytes_received] = '\0';
  75.         strcat(response, rcvbuf);
  76.         bzero(rcvbuf, sizeof(rcvbuf));
  77.     }
  78.     if (bytes_received == -1) {
  79.         fprintf(stderr, "recv error errno:%d,%s", errno, strerror(errno));
  80.         free(response);
  81.         close(socket_fd);
  82.         return 1;
  83.     }
  84.     printf("Response:\n%s\n", response);
  85.     // 处理分块传输编码
  86.     char *json_start = strstr(response, "{"");
  87.     if (json_start == NULL) {
  88.         fprintf(stderr, "Failed to find JSON data\n");
  89.         free(response);
  90.         close(socket_fd);
  91.         return 1;
  92.     }
  93.     // 解析JSON数据
  94.     cJSON *json = cJSON_Parse(json_start);
  95.     if (json == NULL) {
  96.         const char *error_ptr = cJSON_GetErrorPtr();
  97.         if (error_ptr != NULL) {
  98.             fprintf(stderr, "Error before: %s\n", error_ptr);
  99.         }
  100.         free(response);
  101.         close(socket_fd);
  102.         return 1;
  103.     }
  104.     free(response);
  105.     // 提取双鱼座本周运势信息
  106.     cJSON *name = cJSON_GetObjectItem(json, "name");
  107.     cJSON *date = cJSON_GetObjectItem(json, "date");
  108.     cJSON *health = cJSON_GetObjectItem(json, "health");
  109.     cJSON *job = cJSON_GetObjectItem(json, "job");
  110.     cJSON *love = cJSON_GetObjectItem(json, "love");
  111.     cJSON *money = cJSON_GetObjectItem(json, "money");
  112.     cJSON *work = cJSON_GetObjectItem(json, "work");
  113.     if (name == NULL || !cJSON_IsString(name) ||
  114.         date == NULL || !cJSON_IsString(date) ||
  115.         health == NULL || !cJSON_IsString(health) ||
  116.         job == NULL || !cJSON_IsString(job) ||
  117.         love == NULL || !cJSON_IsString(love) ||
  118.         money == NULL || !cJSON_IsString(money) ||
  119.         work == NULL || !cJSON_IsString(work)) {
  120.         fprintf(stderr, "Failed to get one or more fields\n");
  121.         cJSON_Delete(json);
  122.         close(socket_fd);
  123.         return 1;
  124.     }
  125.     // 输出双鱼座本周运势信息
  126.     printf("星座名称: %s\n", name->valuestring);
  127.     printf("日期: %s\n", date->valuestring);
  128.     printf("健康: %s\n", health->valuestring);
  129.     printf("工作: %s\n", job->valuestring);
  130.     printf("爱情: %s\n", love->valuestring);
  131.     printf("财富: %s\n", money->valuestring);
  132.     printf("学业: %s\n", work->valuestring);
  133.     // 释放JSON对象
  134.     cJSON_Delete(json);
  135.     close(socket_fd);
  136.     return 0;
  137. }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册