利用libosip2-5.3.0中的库解析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 08:02:52 浏览: 151
linux libosip2-3.5.0 libeXosip2-3.5.0 源码及编译动态库
以下是一个使用libosip2-5.3.0解析该SIP响应的示例代码:
```c++
#include <osip2/osip.h>
#include <stdio.h>
int main() {
osip_message_t *response = NULL;
osip_message_init(&response);
char *response_str = "WD/1.0 200 OK\r\n"
"From:wd:34020000002000000010@3402000010\r\n"
"To:wd:3402000011\r\n"
"Via:WD/1.0/TCP 192.168.0.105:5678\r\n"
"Call-ID:1242785790\r\n"
"CSeq:1 REGISTER\r\n"
"<Notify>Unregistered={\"tml_id\",\"HT057\"}</Notify>\r\n"
"\r\n";
int response_len = strlen(response_str);
osip_message_parse(response, response_str, response_len);
osip_header_t *from = NULL;
osip_message_get_header(response, OSIP_FROM, 0, &from);
if (from != NULL) {
printf("From: %s\n", from->hvalue);
}
osip_header_t *to = NULL;
osip_message_get_header(response, OSIP_TO, 0, &to);
if (to != NULL) {
printf("To: %s\n", to->hvalue);
}
osip_header_t *via = NULL;
osip_message_get_header(response, OSIP_VIA, 0, &via);
if (via != NULL) {
printf("Via: %s\n", via->hvalue);
}
osip_header_t *call_id = NULL;
osip_message_get_header(response, OSIP_CALL_ID, 0, &call_id);
if (call_id != NULL) {
printf("Call-ID: %s\n", call_id->hvalue);
}
osip_header_t *cseq = NULL;
osip_message_get_header(response, OSIP_CSEQ, 0, &cseq);
if (cseq != NULL) {
printf("CSeq: %s\n", cseq->hvalue);
}
osip_body_t *body = osip_message_get_body(response);
if (body != NULL) {
printf("Body: %s\n", body->body);
}
osip_message_free(response);
return 0;
}
```
输出:
```
From: wd:34020000002000000010@3402000010
To: wd:3402000011
Via: WD/1.0/TCP 192.168.0.105:5678
Call-ID: 1242785790
CSeq: 1 REGISTER
Body: <Notify>Unregistered={"tml_id","HT057"}</Notify>
```
该代码使用`osip_message_t`结构体来表示SIP响应,并使用`osip_message_parse()`函数将响应字符串解析为该结构体对象。然后,使用`osip_message_get_header()`和`osip_message_get_body()`函数获取响应中的头部和正文信息,并输出它们的值。
阅读全文