【完善】GET/POST 例程相关的导出命令

Signed-off-by: chenyong <1521761801@qq.com>
master
chenyong 6 years ago
parent 20aad8f250
commit 0c484f4a53

@ -16,39 +16,15 @@
#define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt" #define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt"
int webclient_get_test(int argc, char **argv) /* send HTTP GET request by common request interface, it used to receive longer data */
static int webclient_get_comm(const char *uri)
{ {
struct webclient_session* session = RT_NULL; struct webclient_session* session = RT_NULL;
unsigned char *buffer = RT_NULL; unsigned char *buffer = RT_NULL;
char *URI = RT_NULL;
int index, ret = 0; int index, ret = 0;
int bytes_read, resp_status; int bytes_read, resp_status;
int content_length = -1; 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_RESP_BUFSZ); buffer = (unsigned char *) web_malloc(GET_RESP_BUFSZ);
if (buffer == RT_NULL) if (buffer == RT_NULL)
{ {
@ -67,14 +43,14 @@ int webclient_get_test(int argc, char **argv)
} }
/* send GET request by default header */ /* send GET request by default header */
if ((resp_status = webclient_get(session, URI)) != 200) if ((resp_status = webclient_get(session, uri)) != 200)
{ {
rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status); rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
ret = -RT_ERROR; ret = -RT_ERROR;
goto __exit; goto __exit;
} }
rt_kprintf("get response data: \n"); rt_kprintf("webclient get response data: \n");
content_length = webclient_content_length_get(session); content_length = webclient_content_length_get(session);
if (content_length < 0) if (content_length < 0)
@ -132,52 +108,23 @@ __exit:
web_free(buffer); web_free(buffer);
} }
if (URI)
{
web_free(URI);
}
return ret; return ret;
} }
/* send HTTP GET request by simplify request interface, it used to received shorter data */
int webclient_get_smpl_test(int argc, char **argv) static int webclient_get_smpl(const char *uri)
{ {
char *URI, *request; char *request = RT_NULL;
int index; int index;
if (argc == 1) if (webclient_request(uri, RT_NULL, RT_NULL, (unsigned char **)&request) < 0)
{
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_smpl_test [URI] - webclient simplify GET request test.\n");
return -1;
}
if (webclient_request(URI, RT_NULL, RT_NULL, (unsigned char **)&request) < 0)
{ {
rt_kprintf("webclient send get request failed."); rt_kprintf("webclient send get request failed.");
web_free(URI); return -RT_ERROR;
return -1;
} }
rt_kprintf("get response data: \n"); rt_kprintf("webclient send get request by simplify request interface.\n");
rt_kprintf("webclient get response data: \n");
for (index = 0; index < rt_strlen(request); index++) for (index = 0; index < rt_strlen(request); index++)
{ {
rt_kprintf("%c", request[index]); rt_kprintf("%c", request[index]);
@ -189,9 +136,65 @@ int webclient_get_smpl_test(int argc, char **argv)
web_free(request); web_free(request);
} }
if (URI) return 0;
}
int webclient_get_test(int argc, char **argv)
{
char *uri = RT_NULL;
if (argc == 1)
{
uri = web_strdup(GET_LOCAL_URI);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create get request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_get_comm(uri);
}
else if (argc == 2)
{
if (rt_strcmp(argv[1], "-s") == 0)
{
uri = web_strdup(GET_LOCAL_URI);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create get request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_get_smpl(uri);
}
else
{
uri = web_strdup(argv[1]);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create get request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_get_comm(uri);
}
}
else if(argc == 3 && rt_strcmp(argv[1], "-s") == 0)
{
uri = web_strdup(argv[2]);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create get request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_get_smpl(uri);
}
else
{ {
web_free(URI); rt_kprintf("web_get_test [URI] - webclient GET request test.\n");
rt_kprintf("web_get_test -s [URI] - webclient simplify GET request test.\n");
return -RT_ERROR;
} }
return 0; return 0;
@ -199,6 +202,5 @@ int webclient_get_smpl_test(int argc, char **argv)
#ifdef FINSH_USING_MSH #ifdef FINSH_USING_MSH
#include <finsh.h> #include <finsh.h>
MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get, web_get [URI] webclient GET request test); MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, webclient get request test);
MSH_CMD_EXPORT_ALIAS(webclient_get_smpl_test, web_get_smpl, web_get_smpl [URI] webclient simplify GET request test);
#endif /* FINSH_USING_MSH */ #endif /* FINSH_USING_MSH */

@ -20,45 +20,20 @@
const char *post_data = "RT-Thread is an open source IoT operating system from China!"; const char *post_data = "RT-Thread is an open source IoT operating system from China!";
int webclient_post_test(int argc, char **argv) /* send HTTP POST request by common request interface, it used to receive longer data */
static int webclient_post_comm(const char *uri, const char *post_data)
{ {
struct webclient_session* session = RT_NULL; struct webclient_session* session = RT_NULL;
unsigned char *buffer = RT_NULL; unsigned char *buffer = RT_NULL;
char *URI = RT_NULL;
int index, ret = 0; int index, ret = 0;
int bytes_read, resp_status; 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); buffer = (unsigned char *) web_malloc(POST_RESP_BUFSZ);
if (buffer == RT_NULL) if (buffer == RT_NULL)
{ {
rt_kprintf("no memory for receive response buffer.\n"); rt_kprintf("no memory for receive response buffer.\n");
ret = -RT_ENOMEM; ret = -RT_ENOMEM;
goto __exit; goto __exit;
} }
/* create webclient session and set header response size */ /* create webclient session and set header response size */
@ -74,14 +49,14 @@ int webclient_post_test(int argc, char **argv)
webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
/* send POST request by default header */ /* send POST request by default header */
if ((resp_status = webclient_post(session, URI, post_data)) != 200) if ((resp_status = webclient_post(session, uri, post_data)) != 200)
{ {
rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status); rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status);
ret = -RT_ERROR; ret = -RT_ERROR;
goto __exit; goto __exit;
} }
rt_kprintf("post response data: \n"); rt_kprintf("webclient post response data: \n");
do do
{ {
bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ); bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ);
@ -109,55 +84,27 @@ __exit:
web_free(buffer); web_free(buffer);
} }
if (URI)
{
web_free(URI);
}
return ret; return ret;
} }
int webclient_post_smpl_test(int argc, char **argv) /* send HTTP POST request by simplify request interface, it used to received shorter data */
static int webclient_post_smpl(const char *uri, const char *post_data)
{ {
char *URI, *request, *header = RT_NULL; char *request = RT_NULL, *header = RT_NULL;
int index; int index;
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_smpl_test [URI] - webclient simplify POST request test.\n");
return -1;
}
webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data)); webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data));
webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n"); webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n");
if (webclient_request(URI, (const char *)header, post_data, (unsigned char **)&request) < 0) if (webclient_request(uri, (const char *)header, post_data, (unsigned char **)&request) < 0)
{ {
rt_kprintf("webclient send post request failed."); rt_kprintf("webclient send post request failed.");
web_free(header); web_free(header);
web_free(URI); return -RT_ERROR;
return -1;
} }
rt_kprintf("post response data: \n"); rt_kprintf("webclient send post request by simplify request interface.\n");
rt_kprintf("webclient post response data: \n");
for (index = 0; index < rt_strlen(request); index++) for (index = 0; index < rt_strlen(request); index++)
{ {
rt_kprintf("%c", request[index]); rt_kprintf("%c", request[index]);
@ -169,20 +116,77 @@ int webclient_post_smpl_test(int argc, char **argv)
web_free(header); web_free(header);
} }
if (URI) if (request)
{ {
web_free(URI); web_free(request);
} }
if (request) return 0;
}
int webclient_post_test(int argc, char **argv)
{
char *uri = RT_NULL;
if (argc == 1)
{ {
web_free(request); uri = web_strdup(POST_LOCAL_URI);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create post request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_post_comm(uri, post_data);
}
else if (argc == 2)
{
if (rt_strcmp(argv[1], "-s") == 0)
{
uri = web_strdup(POST_LOCAL_URI);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create post request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_post_smpl(uri, post_data);
}
else
{
uri = web_strdup(argv[1]);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create post request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_post_comm(uri, post_data);
}
}
else if(argc == 3 && rt_strcmp(argv[1], "-s") == 0)
{
uri = web_strdup(argv[2]);
if(uri == RT_NULL)
{
rt_kprintf("no memory for create post request uri buffer.\n");
return -RT_ENOMEM;
}
webclient_post_smpl(uri, post_data);
}
else
{
rt_kprintf("web_post_test [uri] - webclient post request test.\n");
rt_kprintf("web_post_test -s [uri] - webclient simplify post request test.\n");
return -RT_ERROR;
} }
return 0; return 0;
} }
#ifdef FINSH_USING_MSH #ifdef FINSH_USING_MSH
#include <finsh.h> #include <finsh.h>
MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post, web_post [URI] - webclient POST request test.); MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient post request test.);
MSH_CMD_EXPORT_ALIAS(webclient_post_smpl_test, web_post_smpl, web_post_smpl [URI] - webclient simplify POST request test.);
#endif /* FINSH_USING_MSH */ #endif /* FINSH_USING_MSH */

Loading…
Cancel
Save