#define POST_RESP_BUFSZ 1024
#define POST_HEADER_BUFSZ 1024
#define POST_LOCAL_URI "www.baidu.com"
static int webclient_post_comm(const char *uri, const char *post_data)
{
struct webclient_session* session = RT_NULL;
unsigned char *buffer = RT_NULL;
int index, ret = 0;
int bytes_read, resp_status;
buffer = (unsigned char *) web_malloc(POST_RESP_BUFSZ);
if (buffer == RT_NULL)
{
rt_kprintf("no memory for receive response buffer.\n");
ret = -RT_ENOMEM;
goto __exit;
}
/* create webclient session and set header response size */
session = webclient_session_create(POST_HEADER_BUFSZ);
if (session == RT_NULL)
{
ret = -RT_ENOMEM;
goto __exit;
}
/* build header for upload */
webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data));
webclient_header_fields_add(session, "Content-Type: application/json\r\n");
/* send POST request by default header */
if ((resp_status = webclient_post(session, uri, post_data)) != 200)
{
rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status);
ret = -RT_ERROR;
goto __exit;
}
rt_kprintf("webclient post response data: \n");
do
{
bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ);
if (bytes_read <= 0)
{
break;
}
for (index = 0; index < bytes_read; index++)
{
rt_kprintf("%c", buffer[index]);
}
} while (1);
rt_kprintf("\n");
__exit:
if (session)
{
webclient_close(session);
}
if (buffer)
{
web_free(buffer);
}
return ret;
}
void httponline_test(void){
cJSON *cdata = cJSON_CreateObject();
cJSON *ccar= cJSON_CreateString("False");
cJSON *ccheckStatus = cJSON_CreateString("1");
cJSON_AddItemToObject(cdata, "car", ccar);
cJSON_AddItemToObject(cdata, "checkStatus", ccheckStatus);
cJSON *cupload = cJSON_CreateObject();
cJSON_AddItemToObject(cupload, "data", cdata);
cJSON *ctype = cJSON_CreateNumber(0);
cJSON_AddItemToObject(cupload, "type", ctype);
LOG_D(cJSON_Print(cupload));
char *post_data=cJSON_Print(cupload);
char *uri = RT_NULL;
uri = web_strdup(POST_LOCAL_URI);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create post request uri buffer.\n");
//return -RT_ENOMEM;
}
for(int i=0;i<10;i++){
rt_thread_mdelay(1000);
webclient_post_comm(uri,post_data);
}
}
MSH_CMD_EXPORT(httponline_test, httponline_test);
POST_LOCAL_URI填自己需要上传数据的地址
webclient配置:

其他配置:

原作者:达克尔_Y