HTTP通信:获取图片JSON格式并转化下载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP通信:获取图片JSON格式并转化下载相关的知识,希望对你有一定的参考价值。

#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"




/* http://b.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513d17b6a2ea386d55fbb2fbd9e2.jpg

http://a.hiphotos.baidu.com/image/pic/item/7acb0a46f21fbe091bd6251369600c338744ad29.jpg

http://h.hiphotos.baidu.com/image/pic/item/730e0cf3d7ca7bcb55c3132bbc096b63f624a87e.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;

}


以上是关于HTTP通信:获取图片JSON格式并转化下载的主要内容,如果未能解决你的问题,请参考以下文章

js中获取并遍历php数组

Mac-json数据快速转成plist数据

怎么把图片转化成eps格式

JSON

PHP获取远程线上图片 压缩包格式让用户下载

如何在openlayers中加载json数据?