HTTP高级请求 - HttpRequestEx
函数简介
高级HTTP请求,支持自定义Method、请求头(含Cookie)、请求体。
接口名称
HttpRequestEx
DLL调用
const char* HttpRequestEx(int64_t instance, const char* method, const char* url, const char* headers, const char* body, const char* content_type, int32_t* status_code);
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| method | 字符串 | 方法,如"GET"/"POST"/"PUT"/"DELETE",大小写不敏感 |
| url | 字符串 | 完整URL |
| headers | 字符串 | 自定义请求头,多行字符串,每行"Name: Value",可为空 |
| body | 字符串 | 请求体,GET可传空 |
| content_type | 字符串 | 如"application/json",可为空 |
| status_code | 整数型指针 | 输出HTTP状态码,可为NULL |
示例
// GET请求带自定义请求头
int32_t statusCode = 0;
const char* headers =
"User-Agent: MyApp/1.0\r\n"
"Accept: application/json\r\n"
"Authorization: Bearer token123\r\n";
const char* response = HttpRequestEx(instance,
"GET",
"https://api.example.com/data",
headers,
"",
"",
&statusCode);
printf("状态码:%d\n", statusCode);
if (response != NULL) {
printf("响应:%s\n", response);
FreeStringPtr(instance, response);
}
// POST请求带Cookie
const char* postHeaders =
"Cookie: session=abc123; user=admin\r\n"
"User-Agent: MyApp/1.0\r\n";
const char* jsonBody = "{\"name\":\"test\"}";
const char* postResponse = HttpRequestEx(instance,
"POST",
"https://api.example.com/create",
postHeaders,
jsonBody,
"application/json",
&statusCode);
if (postResponse != NULL) {
FreeStringPtr(instance, postResponse);
}
返回值
字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。
注意事项
- 返回的字符串需要调用
FreeStringPtr释放内存 - headers格式:每行"Name: Value",以"\r\n"分隔
- 支持的HTTP方法:GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS等
- status_code可以为NULL,如果不需要获取状态码
