#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. 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. Sending an HTTP request
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; Get valid picture data after a picture data terminator
File*file = fopen (pic, "w+");
Fwrite (q, recvsize-(q-p), 1, file);
fclose (file);
Free (p);
}
int main (void)
{
1. Connect to the server
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);//server port number
SADDR.SIN_ADDR.S_ADDR = inet_addr ("14.215.177.185");//server address
int ret = connect (sockfd, struct sockaddr*) &saddr, sizeof (SADDR));
if (Ret < 0)
{
Perror ("Connect Fail");
}
2. Sending an HTTP request
Write (SOCKFD, Http_req, strlen (http_req));
3. Receiving server response data
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));
}
Remove HTTP Headers
Q = strstr (P, "\r\n\r\n") +4;
Q = strstr (q, "{");
Remove the data tail
Char *tmp = strstr (q, "}]}\r") +3;
*tmp = ' + ';
printf ("%s\n", Q);
JSON data parsing
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);
Creating threads to download data
pthread_t id = 0;
Pthread_create (&id, NULL, Download_run, (void *) picobj->valuestring);
Usleep (10000);
Pthread_detach (ID);
}
Free (p);
GetChar ();
GetChar ();//In order to not let the main program exit, affect the thread download picture
return 0;
}
HTTP communication: Get picture JSON format and convert download