亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HTTP通信:獲取圖片JSON格式并轉化下載

發布時間:2020-07-17 13:35:29 來源:網絡 閱讀:1972 作者:虎皮喵的喵 欄目:系統運維

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/socket.h>

#include <stdio.h>

#include <linux/in.h>

#include <string.h>

#include <unistd.h>

#include <stdlib.h>

#include "cJSON.h"


//http://image.baidu.com/channel/listjson?pn=0&rn=30&tag1=%E7%BE%8E%E5%A5%B3&tag2=%E5%85%A8%E9%83%A8&ie=utf8


#define HTTP_REQ "GET /channel/listjson?pn=0&rn=3&tag1=%E7%BE%8E%E5%A5%B3&tag2=%E5%85%A8%E9%83%A8&ie=utf8 \

HTTP/1.1\r\nHost: image.baidu.com\r\nConnection: close\r\n\r\n"




/* https://cache.yisu.com/upload/information/20200302/31/2616.jpg

https://cache.yisu.com/upload/information/20200302/31/2617.jpg

https://cache.yisu.com/upload/information/20200302/31/2620.jpg */


#define HTTP_REQ_PIC "GET /image/pic/item/%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n"



void *download_run(void *arg)

{

char *t = (char *)arg;

char buf[strlen(t)+1];

memcpy(buf, t, strlen(t)+1);

char www[64]= {0};

char pic[128] = {0};

sscanf(buf, "http://%[^/]/image/pic/item/%s", www, pic);

printf("%s---%s\n", www, pic);


char requstbuffer[1024] = {0};

sprintf(requstbuffer, HTTP_REQ_PIC, pic, www);

//1.連接服務器

int sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0)

{

return NULL;

}

struct sockaddr_in saddr;

bzero(&saddr, sizeof(saddr));

saddr.sin_family = AF_INET;

     saddr.sin_port = htons(80);//服務器端口號

saddr.sin_addr.s_addr = inet_addr("119.146.74.48");//服務器地址

int ret = connect(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));

if(ret < 0)

{

perror("connect fail");

}

//2.發送http請求

write(sockfd, requstbuffer, strlen(requstbuffer));

char buffer[1024]={0};

char *p = malloc(1024*300);

char *q = p;

unsigned int recvsize = 0;

while(1)

{

int ret = read(sockfd, buffer, 1024);

if(ret <=0 )break;

memcpy(q, buffer,ret);

q+=ret;

recvsize += ret;

memset(buffer, 0, sizeof(buffer));

}

q = strstr(p, "\r\n\r\n")+4;  //獲取圖片數據結束符后面的有效圖片數據

FILE*file = fopen(pic, "w+");

fwrite(q, recvsize-(q-p), 1, file);

fclose(file);

free(p);

}




int main(void)

{

//1.連接服務器

int sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0)

{

return -1;

}

struct sockaddr_in saddr;

bzero(&saddr, sizeof(saddr));

saddr.sin_family = AF_INET;

    saddr.sin_port = htons(80);//服務器端口號

saddr.sin_addr.s_addr = inet_addr("14.215.177.185");//服務器地址

int ret = connect(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));

if(ret < 0)

{

perror("connect fail");

}

//2.發送http請求

write(sockfd, HTTP_REQ, strlen(HTTP_REQ));

//3.接收服務器應答數據

char buffer[1024]={0};

char *p = malloc(2048*30+1200);

char *q = p;

while(1)

{

int ret = read(sockfd, buffer, 1024);

if(ret <=0 )break;

memcpy(q, buffer,ret);

q+=ret;

memset(buffer, 0, sizeof(buffer));

}

//去掉http頭

q = strstr(p, "\r\n\r\n")+4;

q = strstr(q, "{");

//去掉數據尾巴

char *tmp = strstr(q, "}]}\r")+3;

*tmp = '\0';

printf("%s\n", q);

//JSON數據解析

cJSON *obj = cJSON_Parse(q);

cJSON *array = cJSON_GetObjectItem(obj, "data");

int size = cJSON_GetArraySize(array);

int i=0;

for(i=0;i<size-1; i++)

{

cJSON *aobj = cJSON_GetArrayItem(array, i);

cJSON *picObj = cJSON_GetObjectItem(aobj, "image_url");

//printf("%s\n", picObj->valuestring);

//創建線程下載數據

pthread_t id = 0;

pthread_create(&id, NULL, download_run, (void *)picObj->valuestring);

usleep(10000);

pthread_detach(id);

}

free(p);

getchar();

    getchar();//此兩處為了不讓主程序退出,影響線程下載圖片

return 0;

}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

英德市| 名山县| 东丽区| 桓台县| 黄山市| 连南| 项城市| 白山市| 尖扎县| 封丘县| 永修县| 越西县| 和静县| 莲花县| 独山县| 博客| 南涧| 栾城县| 盐池县| 武乡县| 延庆县| 长子县| 吉木乃县| 靖远县| 湘乡市| 岱山县| 晋州市| 罗定市| 伊春市| 高清| 海丰县| 巴彦县| 瑞丽市| 农安县| 通城县| 巴楚县| 正蓝旗| 贵定县| 三都| 凌源市| 静安区|