Merge pull request #34 from Lawlieta/master

【修改】 webclient_request 函数中 POST 请求方式实现
master
朱天龙 (Armink) 7 years ago committed by GitHub
commit c2a8b588ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -168,7 +168,7 @@
|:------------------|:-----------------------------------| |:------------------|:-----------------------------------|
|URI | 连接的 HTTP 服务器地址 | |URI | 连接的 HTTP 服务器地址 |
|header | 需要发送的头部数据 | |header | 需要发送的头部数据 |
| | = NULL发送默认头数据信息只用于发送 GET 请求 | | | = NULL发送默认头数据信息可用于发送 GET/POST请求 |
| | != NULL发送指定头数据信息可用于发送 GET/POST请求 | | | != NULL发送指定头数据信息可用于发送 GET/POST请求 |
|post_data | 发送到服务器的数据 | |post_data | 发送到服务器的数据 |
| | = NULL该发送请求为 GET 请求 | | | = NULL该发送请求为 GET 请求 |

@ -333,9 +333,8 @@ webclient_close(session);
```c ```c
char *post_data = "abcdefg"; char *post_data = "abcdefg";
char *header = "xxx";
webclient_request(URI, header, post_data, NULL); webclient_request(URI, NULL, post_data, NULL);
``` ```
## 常见问题 ## 常见问题

@ -47,8 +47,8 @@ extern "C" {
#define web_strdup rt_strdup #define web_strdup rt_strdup
#endif #endif
#define WEBCLIENT_SW_VERSION "2.0.0" #define WEBCLIENT_SW_VERSION "2.0.1"
#define WEBCLIENT_SW_VERSION_NUM 0x20000 #define WEBCLIENT_SW_VERSION_NUM 0x20001
#define WEBCLIENT_HEADER_BUFSZ 4096 #define WEBCLIENT_HEADER_BUFSZ 4096
#define WEBCLIENT_RESPONSE_BUFSZ 4096 #define WEBCLIENT_RESPONSE_BUFSZ 4096

@ -1416,12 +1416,6 @@ int webclient_request(const char *URI, const char *header, const char *post_data
RT_ASSERT(URI); RT_ASSERT(URI);
if (post_data && header == RT_NULL)
{
LOG_E("request post failed, post input header cannot be empty.");
return -WEBCLIENT_ERROR;
}
if (post_data == RT_NULL && response == RT_NULL) if (post_data == RT_NULL && response == RT_NULL)
{ {
LOG_E("request get failed, get response data cannot be empty."); LOG_E("request get failed, get response data cannot be empty.");
@ -1468,12 +1462,25 @@ int webclient_request(const char *URI, const char *header, const char *post_data
{ {
rt_strncpy(session->header->buffer, header, rt_strlen(header)); rt_strncpy(session->header->buffer, header, rt_strlen(header));
} }
else
{
/* build header for upload */
webclient_header_fields_add(session, "Content-Length: %d\r\n", rt_strlen(post_data));
webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
}
if (webclient_post(session, URI, post_data) != 200) if (webclient_post(session, URI, post_data) != 200)
{ {
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
totle_length = webclient_response(session, response);
if (totle_length <= 0)
{
rc = -WEBCLIENT_ERROR;
goto __exit;
}
} }
if (header != RT_NULL) if (header != RT_NULL)

Loading…
Cancel
Save