#ifndef __HTTPPOST_H
#define __HTTPPOST_H
#include "httppost.h"
#endif
u16 func_pack_httppost_body(char *buff_body, char *productId, char *deviceSn, char *deviceMac, char *devicePassword, char *hardwareVersion)
{
u16 len;
len = sprintf(buff_body, "a=%s&b=%s&c=%s&d=%s&e=%s",\
productId, deviceSn, deviceMac, devicePassword, hardwareVersion);
return len;
}
u16 func_pack_httppost_head_body(char *buff_post, char *url_tail, u8 *host, u16 port, char *body, u16 body_len)
{
u16 len;
len = sprintf(buff_post, "POST %s HTTP/1.1\r\n"
"Connection: close\r\n"
"User-Agent:W5500\r\n"
"Content-Type:application/x-www-form-urlencoded\r\n"
"Host: %d.%d.%d.%d:%d\r\n"
"Content-Length: %d\r\n\r\n"
"%s\r\n", url_tail, host[0], host[1], host[2], host[3], port, body_len, body
);
return len;
}
static u16 local_port = 50000;
u8 func_http_post(u8 sock_no, u8 *rip, u16 port, char *buf_post, u16 len_post,char *buf_recv, u16 *len_recv, u16 timeout_ms)
{
u16 cnt, len;
char *body_cont;
cnt = 0;
for(;;)
{
switch(getSn_SR(sock_no))
{
case SOCK_INIT:
connect(sock_no, rip, port);
break;
case SOCK_ESTABLISHED:
send(sock_no, (u8*)buf_post, len_post);
if(getSn_IR(sock_no) & Sn_IR_CON)
{
setSn_IR(sock_no, Sn_IR_CON);
}
len = getSn_RX_RSR(sock_no);
if(len > 0)
{
memset(buf_recv, 0, len_post);
len = recv(sock_no, (u8*)buf_recv, len);
body_cont = strstr((char*)buf_recv, "HTTP/1.1 200");
if(body_cont == NULL)
{
return 2;
}
body_cont = strstr((char*)buf_recv, "\r\n\r\n");
if(body_cont != NULL)
{
len = strlen(body_cont) - 4;
memcpy(buf_recv, body_cont + 4, len);
buf_recv[len] = '\0';
*len_recv = len;
close(sock_no);
return 0;
}
}
break;
case SOCK_CLOSE_WAIT:
close(sock_no);
break;
case SOCK_CLOSED:
socket(sock_no, Sn_MR_TCP, local_port++, Sn_MR_ND);
if(local_port > 64000)
{
local_port = 50000;
}
break;
}
cnt ++;
if(cnt >= timeout_ms)
{
close(sock_no);
return 1;
}
delay_ms(1);
}
}
———————————————— 版权声明:本文为CSDN博主「Mr_Johhny」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/lnniyunlong99/article/details/105614408/