Procházet zdrojové kódy

【修改】 webclient_request 函数中 POST 请求方式实现

Signed-off-by: chenyong <1521761801@qq.com>
chenyong před 7 roky
rodič
revize
c69c68c11e
4 změnil soubory, kde provedl 20 přidání a 14 odebrání
  1. 3 3
      docs/api.md
  2. 2 3
      docs/user-guide.md
  3. 2 2
      inc/webclient.h
  4. 13 6
      src/webclient.c

+ 3 - 3
docs/api.md

@@ -168,7 +168,7 @@
 |:------------------|:-----------------------------------|
 |URI                | 连接的 HTTP 服务器地址              |
 |header             | 需要发送的头部数据                  |
-|                   | = NULL,发送默认头数据信息,只用于发送 GET 请求       |
+|                   | = NULL,发送默认头数据信息,可用于发送 GET/POST请求 |
 |                   | != NULL,发送指定头数据信息,可用于发送 GET/POST请求  |
 |post_data          | 发送到服务器的数据                  |
 |                   | = NULL,该发送请求为 GET 请求       |
@@ -201,8 +201,8 @@
 |:------------------|:-----------------------------------|
 |session            | 当前连接会话结构体指针               |
 | **返回**          | **描述**                           |
-| `>0`                | Content-Length 字段数据            | 
-| <0                | 获取失败                           | 
+| `>0`                | Content-Length 字段数据            |
+| <0                | 获取失败                           |
 
 ## 下载文件到本地
 

+ 2 - 3
docs/user-guide.md

@@ -83,7 +83,7 @@ if (session == RT_NULL)
  WebClient 软件包提供两种请求头部发送方式:
 
  - 默认头部数据
- 
+
     如果要使用默认的头部信息,则不需要拼接任何头部数据,可直接调用 GET 发送命令。默认头部数据一般只用于 GET 请求。
 
  - 自定义头部数据
@@ -333,9 +333,8 @@ webclient_close(session);
 
 ```c
 char *post_data = "abcdefg";
-char *header = "xxx";
 
-webclient_request(URI, header, post_data, NULL);
+webclient_request(URI, NULL, post_data, NULL);
 ```
 
 ## 常见问题

+ 2 - 2
inc/webclient.h

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

+ 13 - 6
src/webclient.c

@@ -1416,12 +1416,6 @@ int webclient_request(const char *URI, const char *header, const char *post_data
 
     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)
     {
         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));
         }
+        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)
         {
             rc = -WEBCLIENT_ERROR;
             goto __exit;
         }
+        
+        totle_length = webclient_response(session, response);
+        if (totle_length <= 0)
+        {
+            rc = -WEBCLIENT_ERROR;
+            goto __exit;
+        }
     }
 
     if (header != RT_NULL)