渭茱瀑 发表于 2025-6-7 10:10:08

HTTP-获取星座运势

使用http服务向聚合API发送请求获取运势的请求,并对接收到的数据进行JSON解析
/**************************************************************************
*
* 设计http程序,客户端向聚合API发送获取星座运势的请求,并解析出收到数据,使用JSON进行解析
* author:jindouliu2024@163.com
* date:2025.5.22
*
*                   
*
* Copyright (c)2024-2025   jindouliu2024@163.com   All right Reserved
* *************************************************************************/
#include<stdio.h>
#include<time.h>
#include <unistd.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include
#include <unistd.h>
#include<netinet/udp.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <pthread.h>
#include <netdb.h>
#include "cJSON.h"

#define PORT80
#define DNS"web.juhe.cn"
#define KEY "   "//自己从聚合API申请的密钥
#define TYPE "week"
#define CONSNAME "%E5%8F%8C%E9%B1%BC%E5%BA%A7"//双鱼座,可以换成自己的星座

int main(int argc, char *argv[]) {
    char buf = {0};
    char rcvbuf = {0};
    char *response = malloc(4096);
    if (response == NULL) {
      fprintf(stderr, "Memory allocation failed\n");
      return 1;
    }
    bzero(response, 4096);
    struct hostent *host;
    // 创建套接字文件
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_fd == -1){
      fprintf(stderr,"socket error errno:%d,%s",errno,strerror(errno));
      return 1;
    }
    host = gethostbyname(DNS);
    if(host == NULL){
      fprintf(stderr,"gethostbyname error errno:%d,%s",errno,strerror(errno));
      return 2;
    }

    // 绑定服务器的端口和地址
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr =((struct in_addr *)(host->h_addr_list))->s_addr;
   
    // 申请连接
    int flag = connect(socket_fd,(struct sockaddr *)&server,sizeof(server));
    if(flag == -1){
      fprintf(stderr,"connect error errno:%d,%s",errno,strerror(errno));
      return 1;
    }
   
    // HTTP协议格式
    sprintf(buf,"GET /constellation/getAll?key=%s&consName=%s&type=%s HTTP/1.1\r\n"
            "Host: web.juhe.cn\r\n"
            "Connection: close\r\n"
            "\r\n", KEY, CONSNAME, TYPE);
    // 发送请求
    send(socket_fd, buf, strlen(buf), 0);
    // 接收响应数据
    int bytes_received;
    while ((bytes_received = recv(socket_fd, rcvbuf, sizeof(rcvbuf) - 1, 0)) > 0) {
      rcvbuf = '\0';
      strcat(response, rcvbuf);
      bzero(rcvbuf, sizeof(rcvbuf));
    }
    if (bytes_received == -1) {
      fprintf(stderr, "recv error errno:%d,%s", errno, strerror(errno));
      free(response);
      close(socket_fd);
      return 1;
    }
    printf("Response:\n%s\n", response);

    // 处理分块传输编码
    char *json_start = strstr(response, "{\"");
    if (json_start == NULL) {
      fprintf(stderr, "Failed to find JSON data\n");
      free(response);
      close(socket_fd);
      return 1;
    }

    // 解析JSON数据
    cJSON *json = cJSON_Parse(json_start);
    if (json == NULL) {
      const char *error_ptr = cJSON_GetErrorPtr();
      if (error_ptr != NULL) {
            fprintf(stderr, "Error before: %s\n", error_ptr);
      }
      free(response);
      close(socket_fd);
      return 1;
    }
    free(response);

    // 提取双鱼座本周运势信息
    cJSON *name = cJSON_GetObjectItem(json, "name");
    cJSON *date = cJSON_GetObjectItem(json, "date");
    cJSON *health = cJSON_GetObjectItem(json, "health");
    cJSON *job = cJSON_GetObjectItem(json, "job");
    cJSON *love = cJSON_GetObjectItem(json, "love");
    cJSON *money = cJSON_GetObjectItem(json, "money");
    cJSON *work = cJSON_GetObjectItem(json, "work");

    if (name == NULL || !cJSON_IsString(name) ||
      date == NULL || !cJSON_IsString(date) ||
      health == NULL || !cJSON_IsString(health) ||
      job == NULL || !cJSON_IsString(job) ||
      love == NULL || !cJSON_IsString(love) ||
      money == NULL || !cJSON_IsString(money) ||
      work == NULL || !cJSON_IsString(work)) {
      fprintf(stderr, "Failed to get one or more fields\n");
      cJSON_Delete(json);
      close(socket_fd);
      return 1;
    }

    // 输出双鱼座本周运势信息
    printf("星座名称: %s\n", name->valuestring);
    printf("日期: %s\n", date->valuestring);
    printf("健康: %s\n", health->valuestring);
    printf("工作: %s\n", job->valuestring);
    printf("爱情: %s\n", love->valuestring);
    printf("财富: %s\n", money->valuestring);
    printf("学业: %s\n", work->valuestring);

    // 释放JSON对象
    cJSON_Delete(json);
    close(socket_fd);

    return 0;
}
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: HTTP-获取星座运势