HTTP-②GET、POST说明文档

1.HTTP单文件下载(GET)说明文档

在这里插入图片描述

1):http_body_obj <<<<<

typedef struct _http_body_obj{
    char *p;  //用户malloc内存指针
    unsigned int buf_len;  //一个接收缓冲区的大小
    unsigned int recv_len;   //已经收到的字节数
    unsigned char buf_count; //计数接收缓冲区
}http_body_obj;

2):httpcli_ctx 需要配置的参数<<<<<

typedef struct _httpcli_ctx{
    ...
    const char *url;  //url
    ...
    void *priv;  //指向回调函数的私有数据的指针
    ...
    char *connection;   // close or keep-alive
    ...
}http_ctx;

3):相关参数设定

http_body_buf.recv_len = 0;  //初始化为0
http_body_buf.buf_len = BUFFER_SIZE;   //缓冲区大小设置BUFFER_SIZE
http_body_buf.buf_count = 1;   //计数初值1
//分配内存
http_body_buf.p = (char *)malloc(http_body_buf.buf_len * \
                                            sizeof(char));
get_ctx.url = url;   //记录url
get_ctx.priv = &http_body_buf;    //指向http_body_buf
get_ctx.connection = “close”;   //下载结束后close
int ret = httpcli_get(&get_ctx);   //传递句柄调用入口函数
if(ret == HEPROR_OK){   //成功返回
   //输出下载数据
printf(“buffer->%s recv_len:%d\n”,http_body_buf.p,
                                    http_body_buf.recv_len);
httpcli_close(&get_ctx); //关闭

4):例程

//   -----  mp3下载至SD卡   -----
#include "http/http_cli.h"
#include "storage_device.h"
#define MP3_1 "http://xmdx.sc.chinaz.net/Files/DownLoad/sound1 \
/201902/11202.mp3"//273k
#define MP3_2 "http://xmdx.sc.chinaz.net/Files/DownLoad/sound1 \
/201903/11238.mp3"//27k
#define MP3_3 "http://xmdx.sc.chinaz.net/Files/DownLoad/sound1 \
/201901/11054.mp3"//155k
#define BUFFER_SIZE 275*1024 
#define URL_MAX_SIZE 512 
int http_get_test(void)
{
    char *url;
    int ret = 0;
    http_body_obj http_body_buf;
    httpcli_ctx get_ctx;
	void *fd = NULL;

	puts("http_get down start!");
    url = (char *)malloc(URL_MAX_SIZE);
    if (!url) {
        puts(" Error in malloc(url)");
        ret = HERROR_MEM;
		goto __exit;
    }

	sprintf(url,MP3_1);//MP3 down addr
    printf("url->%s\n", url);
    memset(&http_body_buf, 0x0, sizeof(http_body_obj));
    memset(&get_ctx, 0x0, sizeof(httpcli_ctx));
    http_body_buf.recv_len = 0;
    http_body_buf.buf_len = BUFFER_SIZE;
    http_body_buf.buf_count = 1;
    http_body_buf.p = (char *) malloc(http_body_buf.buf_len * \
                                                sizeof(char));
	if (!http_body_buf.p) {
        puts(" Error in malloc(http_body_buf.p)");
        ret = HERROR_MEM;
		goto __exit;
    }

    get_ctx.url = url;
    get_ctx.priv = &http_body_buf;
    get_ctx.connection = "close";
    ret = httpcli_get(&get_ctx);
    if (ret == HERROR_OK) {
		if (BUFFER_SIZE > http_body_buf.recv_len) {
    	    if (storage_device_ready()) {
				fd = fopen("storage/sd0/C/1.mp3","w+");
				if(!fd){
					puts("open file fail ");
				}
				fwrite(fd,http_body_buf.p,http_body_buf.recv_len);
				fclose(fd);
			}else{
				puts("SD not online");
			}	
		}else{
			ret = -1;
		}
    }

__exit:
	httpcli_close(&get_ctx);
    if (http_body_buf.p) {
        free(http_body_buf.p);
    }
	if(url){
		free(url);
	}
	if (ret == HERROR_OK) {
		puts("http_get down success!");
    }
	puts("http_get down over!");
	return ret;
}

2.HTTP上传连接(POST)说明文档

在这里插入图片描述

1):http_body_obj <<<<<

//同上

2):httpcli_ctx需要配置的参数 <<<<<

typedef struct _httpcli_ctx{
    ...
    const char *url;  //url
    ...
    const char *user_http_header; //用户传入的HTTP头
    ...
    void *priv;  //指向回调函数的私有数据的指针
    const char *post_data; //传输的数据
    ...
    int data_len;     //传输数据的长度
    int timeout_millsec;  //超时时间
    ...
}http_ctx;

3):相关参数设定

http_body_buf.recv_len = 0;  //初始化为0
http_body_buf.buf_len = BUFFER_SIZE;   //缓冲区大小设置BUFFER_SIZE
http_body_buf.buf_count = 1;   //计数初值1
//分配内存
http_body_buf.p = (char *)malloc(http_body_buf.buf_len * \
                                            sizeof(char));
	sprintf(url_buf,http_head_tab,strlen(http_post_data));
 post_ctx.url = URL_POST; //url
	post_ctx.user_http_header = url_buf; //http heard
post_ctx.post_data = http_post_data; //传输数据
post_ctx.data_len = strlen(http_post_data); //传输数据长度
post_ctx.timeout_millsec = 10000;  //超时时间
post_ctx.priv = &http_body_buf;
   //输出下载数据
printf(“buffer->%s recv_len:%d\n”,http_body_buf.p,
                                    http_body_buf.recv_len);
httpcli_close(&get_ctx); //关闭

4):例程

//   -----  访问博客园   -----
#define URL_BUF_SIZE 1024 
#define URL_POST "http://61.147.124.120"
static const char http_head_tab[] =
		"POST /webservices/qqOnlineWebService.asmx/qqCheckOnlin \
                                              e HTTP/1.1\n"
		"Host: www.webxml.com.cn\nContent-Type: application/x-w \
                                      ww-form-urlencoded\n"
		"Connection: close\n"
		"Content-Length: %d\n\n";
static const char http_post_data[] =
    "qqCode=12345678\r\n\r\n";
int http_post_test(void)
{
	int ret = 0;
	char *url_buf = NULL;
	httpcli_ctx post_ctx;
    http_body_obj http_body_buf;
	
	puts("http_post start!");
	url_buf = calloc(1,URL_BUF_SIZE);
	if(!url_buf){
        puts(" Error in calloc(url_buf)");
        ret = HERROR_MEM;
		goto __exit;
	}
    memset(&http_body_buf, 0x0, sizeof(http_body_obj));
	memset(&post_ctx, 0x0, sizeof(httpcli_ctx));

    http_body_buf.recv_len = 0;
    http_body_buf.buf_len = BUFFER_SIZE;
    http_body_buf.buf_count = 1;
    http_body_buf.p = (char *) malloc(http_body_buf.buf_len * \
                                                sizeof(char));
    if (http_body_buf.p == NULL) {
        puts(" Error in malloc(http_body_buf.p)\n");
        ret = HERROR_MEM;
		goto __exit;
    }
	
	sprintf(url_buf,http_head_tab,strlen(http_post_data));
    post_ctx.url = URL_POST;
	post_ctx.user_http_header = url_buf;
	post_ctx.post_data = http_post_data;
	post_ctx.data_len = strlen(http_post_data);
	post_ctx.timeout_millsec = 10000;
    post_ctx.priv = &http_body_buf;

    ret = httpcli_post(&post_ctx);
	if (ret == HERROR_OK) {
		puts("http_post success!");
		puts("recv data :");
		for(int i = 0;i<http_body_buf.recv_len;i++)
		{
			printf("%c",http_body_buf.p[i]);
		}
    }
    
__exit:
	httpcli_close(&post_ctx);
    if(http_body_buf.p) {
        free(http_body_buf.p);
    }
    if(url_buf) {
        free(url_buf);
    }
	puts("http_post over!");
    return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值