请教 httpclient 如何 Post 数据

发布于 2019-05-28 11:04:10    浏览:818
    本帖最后由 aozima 于 2019-5-28 11:23 编辑


这是我们目前做的应用开发 把照片post到指定的服务器 ,目前没有头绪,请大神知道
微信图片_20190528110311.png

查看更多

13 个回答
wangbo
wangbo 2019-05-28
This guy hasn't written anything yet
目前使用PKG webclient v1.00 PKG Cjson V1.02
aozima
aozima 2019-05-28
拒绝白嫖,拒绝键盘侠!
    本帖最后由 aozima 于 2019-5-28 11:36 编辑


open/write/close

示例中有post和文件上传的。

另外,楼主给的文档不详细哦,反正我是看不明白。
让出文档的给出个示例或CURL的命令示例出来。

既然post数据类型是表单,那这个json格式又在哪里?表单名又是什么?
文件的表单名又是什么?
文档中都没有说

让给个测试用的html文件也可以的

wangbo
wangbo 2019-05-28
This guy hasn't written anything yet
能在详细一点吗?
wangbo
wangbo 2019-05-28
This guy hasn't written anything yet
接口参数属于哪一部分?是放在header里面 还是 uri中?
Cheney_Chen
Cheney_Chen 2019-05-28
This guy hasn't written anything yet
为啥不用最新版本 Webclient 2.1.0,最新版有详细的文档说明怎么使用,也有 GET POST示例文件参考
wangbo
wangbo 2019-05-31
This guy hasn't written anything yet
目前用的最新版本 Webclient 2.1.0

#include
#include

#include
#include

#include

#define DBG_ENABLE
#define DBG_SECTION_NAME "web.file"
#ifdef WEBCLIENT_DEBUG
#define DBG_LEVEL DBG_LOG
#else
#define DBG_LEVEL DBG_INFO
#endif /* WEBCLIENT_DEBUG */
#define DBG_COLOR
#include

unsigned char Response[4096];
size_t responselen;

int webclient_post_pic(const char* URI, const char* filename,
const char* form_data)
{
int total_write = 0;
size_t length;
char boundary[60];
int fd = -1, rc = WEBCLIENT_OK;
char *header = RT_NULL, *header_ptr;
unsigned char *buffer = RT_NULL, *buffer_ptr;
struct webclient_session* session = RT_NULL;

fd = open(filename, O_RDONLY, 0);
if (fd < 0)
{
LOG_D("post file failed, open file(%s) error.", filename);
rc = -WEBCLIENT_FILE_ERROR;
goto __exit;
}

/* get the size of file */
length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);

buffer = (unsigned char *) web_malloc(WEBCLIENT_RESPONSE_BUFSZ);
if (buffer == RT_NULL)
{
LOG_D("post file failed, no memory for response buffer.");
rc = -WEBCLIENT_NOMEM;
goto __exit;
}

header = (char *) web_malloc(WEBCLIENT_HEADER_BUFSZ);
if (header == RT_NULL)
{
LOG_D("post file failed, no memory for header buffer.");
rc = -WEBCLIENT_NOMEM;
goto __exit;
}
header_ptr = header;

/* build boundary */
rt_snprintf(boundary, sizeof(boundary), "----------------------------%012d", rt_tick_get());

/* build encapsulated mime_multipart information*/
buffer_ptr = buffer;
/*¼üÖµ first boundary */
buffer_ptr += rt_snprintf((char*) buffer_ptr,
WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer), "--%s\r\n", boundary);
buffer_ptr += rt_snprintf((char*) buffer_ptr,
WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer),
//"Content-Type: application/json;charset=utf-8\r\n\r\n");
"Content-Type: raw\r\n\r\n");
buffer_ptr += rt_snprintf((char*) buffer_ptr,
WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer),
"{\"ID\":\"9020\",\"User\":\"\",\"PSW\":\"\",\"MODE\":\"0\"}\r\n");
/* ÕÕƬ*/
buffer_ptr += rt_snprintf((char*) buffer_ptr,
WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer), "--%s\r\n", boundary);
buffer_ptr += rt_snprintf((char*) buffer_ptr,
WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer),
"Content-Disposition: form-data; %s\r\n", form_data);
buffer_ptr += rt_snprintf((char*) buffer_ptr,
WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer),
"Content-Type: application/octet-stream\r\n\r\n");
/* calculate content-length ¼ÆËãbody³¤¶È */
length += buffer_ptr - buffer;
length += rt_strlen(boundary) + 6; /* add the last boundary */

/* build header for upload */
//header_ptr += rt_snprintf(header_ptr,
// WEBCLIENT_HEADER_BUFSZ - (header_ptr - header),
// "Content-Length: %d\r\n", length);
//header_ptr += rt_snprintf(header_ptr,
// WEBCLIENT_HEADER_BUFSZ - (header_ptr - header),
// "Content-Type: multipart/form-data; boundary=%s\r\n", boundary);

session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
if(session == RT_NULL)
{
rc = -WEBCLIENT_NOMEM;
goto __exit;
}

session->header->buffer = header;

//rc = webclient_post(session, URI, NULL);
rc = webclient_connect(session, URI);
if(rc != WEBCLIENT_OK)
{
goto __exit;
}
/* ×éÍ·*/
if (webclient_header_fields_add(session, "POST %s HTTP/1.1\r\n", session->req_url) < 0)
return -WEBCLIENT_NOMEM;
if (webclient_header_fields_add(session, "Host: %s\r\n", session->host) < 0)
return -WEBCLIENT_NOMEM;
if (webclient_header_fields_add(session, "Content-Length: %d\r\n", length) < 0)
return -WEBCLIENT_NOMEM;
if (webclient_header_fields_add(session, "Content-Type: multipart/form-data; boundary=%s\r\n\r\n", boundary) < 0)
return -WEBCLIENT_NOMEM;

webclient_write(session, (unsigned char *) session->header->buffer, session->header->length);

/* send mime_multipart */
webclient_write(session, buffer, buffer_ptr - buffer);

/* send file data */
while (1)
{
length = read(fd, buffer, WEBCLIENT_RESPONSE_BUFSZ);
if (length <= 0)
{
break;
}

total_write = webclient_write(session, buffer, length);
if(total_write < 0)
{
total_write = 0;
}
}

/* send last boundary */
rt_snprintf((char*) buffer, WEBCLIENT_RESPONSE_BUFSZ, "\r\n--%s--\r\n", boundary);
webclient_write(session, buffer, rt_strlen(boundary) + 6);

responselen = webclient_read(session,Response,4096);
rt_kprintf("%s",Response);
if(bblen)
{
bblen = 0;
}

__exit:
if (fd >= 0)
{
close(fd);
}

if (session != RT_NULL)
{
webclient_close(session);
}

if (buffer != RT_NULL)
{
web_free(buffer);
}

//if (header != RT_NULL)
//{
// web_free(header);
//}

return 0;
}

#define POST_URL ";
#define FORM_DATA "name=\"Photo\"; filename=\"902020190529094350.jpg\""
void wpost()
{
webclient_post_pic(POST_URL,"/902020190529095230.jpg",FORM_DATA);
}

#ifdef FINSH_USING_MSH
#include
FINSH_FUNCTION_EXPORT(wpost, Post file by URL)
#endif /* FINSH_USING_MSH */

wangbo
wangbo 2019-05-31
This guy hasn't written anything yet
服务器回应 http/1.1 404 NOT FOUND
wangbo
wangbo 2019-05-31
This guy hasn't written anything yet
我的语法有错误?
wangbo
wangbo 2019-05-31
This guy hasn't written anything yet
同时post请求 键值 照片上传
Cheney_Chen
Cheney_Chen 2019-06-01
This guy hasn't written anything yet
1. webclient_header_fields_add(session, "POST %s HTTP/1.1\r\n", session->req_url) 这段的拼接不需要,webclient_header_fields_add 函数只是用来拼接头部数据的,这段属于请求行;

2. 开启 webclient 软件包 log 功能,可以查看你发送请求、接收响应的的头部信息和数据;

3.如果有条件可以尝试抓包查看收发数据确认问题。

撰写答案

请登录后再发布答案,点击登录
关注者
0
被浏览
818

发布
问题

分享
好友

手机
浏览

扫码手机浏览