commit
64891bc5c7
@ -1,9 +1,13 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
CPPPATH = [cwd]
|
||||
cwd = GetCurrentDir()
|
||||
path = [cwd + '/inc']
|
||||
|
||||
group = DefineGroup('WebClient', src, depend = ['PKG_USING_WEBCLIENT'], CPPPATH = CPPPATH)
|
||||
src = Glob('src/*.c')
|
||||
|
||||
if GetDepend(['WEBCLIENT_USING_SAMPLES']):
|
||||
src += Glob('samples/*.c')
|
||||
|
||||
group = DefineGroup('WebClient', src, depend = ['PKG_USING_WEBCLIENT'], CPPPATH = path)
|
||||
|
||||
Return('group')
|
||||
|
@ -0,0 +1,161 @@
|
||||
/*
|
||||
* File : webclient_get_sample.c
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-03 chenyong the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <webclient.h>
|
||||
|
||||
#define GET_HEADER_BUFSZ 1024
|
||||
#define GET_RESP_BUFSZ 1024
|
||||
|
||||
#define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt"
|
||||
|
||||
int webclient_get_test(int argc, char **argv)
|
||||
{
|
||||
struct webclient_session* session = RT_NULL;
|
||||
unsigned char *buffer = RT_NULL;
|
||||
char *URI = RT_NULL;
|
||||
int index, ret = 0;
|
||||
int bytes_read, resp_status;
|
||||
int content_length = -1;
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
URI = web_strdup(GET_LOCAL_URI);
|
||||
if(URI == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for create URI buffer.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
URI = web_strdup(argv[1]);
|
||||
if(URI == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for create URI buffer.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("webclient_get_test [URI] - webclient GET request test.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer = (unsigned char *) web_malloc(GET_HEADER_BUFSZ);
|
||||
if (buffer == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for receive buffer.\n");
|
||||
ret = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
|
||||
}
|
||||
|
||||
/* create webclient session and set header response size */
|
||||
session = webclient_session_create(GET_HEADER_BUFSZ);
|
||||
if (session == RT_NULL)
|
||||
{
|
||||
ret = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send GET request by default header */
|
||||
if ((resp_status = webclient_get(session, URI)) != 200)
|
||||
{
|
||||
rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
|
||||
ret = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
rt_kprintf("webclient GET request response data :");
|
||||
|
||||
if(webclient_header_fields_get(session, "Content-Length"))
|
||||
{
|
||||
content_length = atoi(webclient_header_fields_get(session, "Content-Length"));
|
||||
}
|
||||
|
||||
if (content_length < 0)
|
||||
{
|
||||
rt_kprintf("The webclient GET request type is chunked.\n");
|
||||
do
|
||||
{
|
||||
bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
|
||||
if (bytes_read <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
for (index = 0; index < bytes_read; index++)
|
||||
{
|
||||
rt_kprintf("%c", buffer[index]);
|
||||
}
|
||||
} while (1);
|
||||
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
int content_pos = 0;
|
||||
|
||||
do
|
||||
{
|
||||
bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
|
||||
if (bytes_read <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
for (index = 0; index < bytes_read; index++)
|
||||
{
|
||||
rt_kprintf("%c", buffer[index]);
|
||||
}
|
||||
|
||||
content_pos += bytes_read;
|
||||
} while (content_pos < content_length);
|
||||
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (session)
|
||||
{
|
||||
webclient_close(session);
|
||||
}
|
||||
|
||||
if (buffer)
|
||||
{
|
||||
web_free(buffer);
|
||||
}
|
||||
|
||||
if (URI)
|
||||
{
|
||||
web_free(URI);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#include <finsh.h>
|
||||
MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, web_get_test [URI] webclient GET request test);
|
||||
#endif /* FINSH_USING_MSH */
|
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* File : webclient_post_sample.c
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-03 chenyong the first version
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <webclient.h>
|
||||
|
||||
#define POST_RESP_BUFSZ 1024
|
||||
#define POST_HEADER_BUFSZ 1024
|
||||
|
||||
#define POST_LOCAL_URI "http://www.rt-thread.com/service/echo"
|
||||
|
||||
const char *post_data = "RT-Thread is an open source IoT operating system from China!";
|
||||
|
||||
int webclient_post_test(int argc, char **argv)
|
||||
{
|
||||
struct webclient_session* session = RT_NULL;
|
||||
unsigned char *buffer = RT_NULL;
|
||||
char *URI = RT_NULL;
|
||||
int index, ret = 0;
|
||||
int bytes_read, resp_status;
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
URI = web_strdup(POST_LOCAL_URI);
|
||||
if(URI == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for create URI buffer.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
URI = web_strdup(argv[1]);
|
||||
if(URI == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for create URI buffer.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("webclient_post_test [URI] - webclient POST request test.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
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/octet-stream\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;
|
||||
}
|
||||
|
||||
LOG_I("webclient POST request response data :");
|
||||
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);
|
||||
}
|
||||
|
||||
if (URI)
|
||||
{
|
||||
web_free(URI);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#include <finsh.h>
|
||||
MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient_post_test [URI] - webclient POST request test.);
|
||||
#endif /* FINSH_USING_MSH */
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue