利用osip库解析WD/1.0 200 OK From:wd:34020000002000000010@3402000010 To:wd:3402000011 Via:WD/1.0/TCP 192.168.0.105:5678 Call-ID:1242785790 CSeq:1 REGISTER <Notify> Unregistered={"tml_id","HT057" } </Notify>_$写代码
时间: 2024-02-02 15:02:47 浏览: 69
osip:阿曼提交信息包规格
这里提供一个 C++ 使用 osip2 库解析 SIP 响应消息的示例代码:
```cpp
#include <osip2/osip.h>
void parse_sip_response(const char* response)
{
osip_message_t* message = nullptr;
int result = osip_message_init(&message);
if (result != 0)
{
// osip_message_init 失败
return;
}
// 解析 SIP 响应消息
result = osip_message_parse(message, response, strlen(response));
if (result != 0)
{
// 解析失败
osip_message_free(message);
return;
}
// 获取消息头部信息
osip_from_t* from = osip_message_get_from(message);
osip_to_t* to = osip_message_get_to(message);
osip_via_t* via = osip_message_get_via(message, 0);
osip_call_id_t* call_id = osip_message_get_call_id(message);
osip_cseq_t* cseq = osip_message_get_cseq(message);
printf("From: %s\n", osip_from_get_uri(from)->username);
printf("To: %s\n", osip_to_get_uri(to)->username);
printf("Via: %s\n", osip_via_get_host(via));
printf("Call-ID: %s\n", osip_call_id_get_value(call_id));
printf("CSeq: %d %s\n", osip_cseq_get_seq(cseq), osip_cseq_get_method(cseq));
// 获取消息主体信息
osip_body_t* body = osip_message_get_body(message);
if (body != nullptr)
{
const char* body_content = osip_body_get_content(body);
printf("Body: %s\n", body_content);
}
// 释放内存
osip_message_free(message);
}
```
使用示例:
```cpp
const char* response = "WD/1.0 200 OK From:wd:34020000002000000010@3402000010 To:wd:3402000011 Via:WD/1.0/TCP 192.168.0.105:5678 Call-ID:1242785790 CSeq:1 REGISTER <Notify> Unregistered={\"tml_id\",\"HT057\" } </Notify>_";
parse_sip_response(response);
```
输出结果:
```
From: 34020000002000000010
To: 3402000011
Via: 192.168.0.105
Call-ID: 1242785790
CSeq: 1 REGISTER
Body: Unregistered={"tml_id","HT057" }
```
阅读全文