HTTP communication: get pictures in JSON format and convert them to download
# include
# include
# include
# include
# include
# include
# include
# include
# include
# 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\ 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 murmuri% s\ n", www, pic)
Char requstbuffer [1024] = {0}
Sprintf (requstbuffer, HTTP_REQ_PIC, pic, www)
/ / 1. Connect to the server
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); / / Server port number
Saddr.sin_addr.s_addr = inet_addr ("119.146.74.48"); / / server address
Int ret = connect (sockfd, (struct sockaddr*) & saddr, sizeof (saddr))
If (ret < 0)
{
Perror ("connect fail")
}
/ / 2. Send http request
Write (sockfd, requstbuffer, strlen (requstbuffer))
Char buffer [1024] = {0}
Char * p = malloc (10240300)
Char * Q = p
Unsigned int recvsize = 0
While (1)
{
Int ret = read (sockfd, buffer, 1024)
If (ret