用mbedtls 发送multipart/form-data post{ "did": "string", "productToken": "string", "state": 0 }
时间: 2024-06-02 14:05:32 浏览: 261
C# 使用multipart form-data方式post数据到服务器
5星 · 资源好评率100%
mbedtls是一个轻量级的加密库,主要提供了TLS/SSL协议实现和加密算法的实现。它并不直接提供发送HTTP请求的功能,但可以结合其他HTTP库(如libcurl)来发送HTTP请求。
对于multipart/form-data格式的POST请求,需要在请求头中指定Content-Type为multipart/form-data,并在请求体中按照格式组织数据。具体实现方法可以参考下面的示例代码:
```c
#include "mbedtls/net_sockets.h"
#include "mbedtls/ssl.h"
#include "mbedtls/error.h"
#include "mbedtls/debug.h"
#include <stdio.h>
#include <string.h>
#define HOSTNAME "example.com"
#define PORT "443"
#define REQUEST_BODY "--boundary\r\nContent-Disposition: form-data; name=\"did\"\r\n\r\nstring\r\n--boundary\r\nContent-Disposition: form-data; name=\"productToken\"\r\n\r\nstring\r\n--boundary\r\nContent-Disposition: form-data; name=\"state\"\r\n\r\n0\r\n--boundary--\r\n"
int main()
{
int ret;
mbedtls_net_context server_fd;
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_x509_crt cacert;
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_x509_crt_init(&cacert);
ret = mbedtls_x509_crt_parse_file(&cacert, "ca.crt");
if (ret != 0) {
printf("failed to parse ca.crt, ret=%d\n", ret);
goto exit;
}
ret = mbedtls_net_connect(&server_fd, HOSTNAME, PORT, MBEDTLS_NET_PROTO_TCP);
if (ret != 0) {
printf("failed to connect, ret=%d\n", ret);
goto exit;
}
ret = mbedtls_ssl_config_defaults(&conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT);
if (ret != 0) {
printf("failed to set ssl config defaults, ret=%d\n", ret);
goto exit;
}
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
ret = mbedtls_ssl_setup(&ssl, &conf);
if (ret != 0) {
printf("failed to setup ssl, ret=%d\n", ret);
goto exit;
}
mbedtls_ssl_set_hostname(&ssl, HOSTNAME);
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
printf("failed to handshake with server, ret=%d\n", ret);
goto exit;
}
}
printf("handshake success!\n");
// send POST request
const char *request_fmt =
"POST /api/v1/device/bind HTTP/1.1\r\n"
"Host: %s:%s\r\n"
"Content-Type: multipart/form-data; boundary=boundary\r\n"
"Content-Length: %d\r\n\r\n%s";
char request_buf[4096];
int request_len = snprintf(request_buf, sizeof(request_buf), request_fmt, HOSTNAME, PORT, strlen(REQUEST_BODY), REQUEST_BODY);
while ((ret = mbedtls_ssl_write(&ssl, (const unsigned char *)request_buf, request_len)) <= 0) {
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
continue;
}
printf("failed to write data to server, ret=%d\n", ret);
goto exit;
}
exit:
mbedtls_x509_crt_free(&cacert);
mbedtls_ssl_free(&ssl);
mbedtls_ssl_config_free(&conf);
mbedtls_net_free(&server_fd);
return 0;
}
```
相关问题:
1. 如何使用mbedtls发送HTTP请求?
2. multipart/form-data格式的POST请求具体怎么组织数据?
3. 如何解析HTTP响应?
阅读全文