Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

HTTP communication: get pictures in JSON format and convert them to download

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

# 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report