HTTP POST请求 - HttpPost
函数简介
发送简单的HTTP POST请求,返回响应体字符串。
接口名称
HttpPost
DLL调用
const char* HttpPost(int64_t instance, const char* url, const char* body, const char* content_type);
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| url | 字符串 | 完整URL |
| body | 字符串 | 请求体内容 |
| content_type | 字符串 | Content-Type,例如"application/json" |
示例
// 发送JSON数据
const char* jsonData = "{\"name\":\"张三\",\"age\":25}";
const char* response = HttpPost(instance,
"https://api.example.com/users",
jsonData,
"application/json");
if (response != NULL) {
printf("响应:%s\n", response);
FreeStringPtr(instance, response);
}
// 发送表单数据
const char* formData = "username=admin&password=123456";
const char* result = HttpPost(instance,
"https://example.com/login",
formData,
"application/x-www-form-urlencoded");
if (result != NULL) {
printf("登录结果:%s\n", result);
FreeStringPtr(instance, result);
}
返回值
字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。
注意事项
- 返回的字符串需要调用
FreeStringPtr释放内存 - content_type必须与body内容匹配
- 常用Content-Type:
application/json、application/x-www-form-urlencoded、application/xml、text/plain - 如需自定义请求头或Cookie,请使用
HttpRequestEx
