libcurl通过HTTPS方式提交XML并解析响应信息

2024-03-12 00:08

本文主要是介绍libcurl通过HTTPS方式提交XML并解析响应信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

不用太多解释,需要的自然有用。稍微有一丝难度的是某个地方用到回调函数,关于回调函数的概念,请百度。

程序中用到XPath,不了解的可以看这里:
http://www.w3school.com.cn/xpath/index.asp

还有这里:
http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html

#include <stdio.h> #include <string.h> #include <curl/curl.h> #include <time.h> #include <libxml/parser.h> #include <libxml/xmlmemory.h> #include <libxml/xpath.h> #include <libxml/xpathInternals.h> static void set_prop(xmlXPathContextPtr context, const xmlChar *xpath, const xmlChar *name, const xmlChar *value){ xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context); if (result) { xmlSetProp(result->nodesetval->nodeTab[0], (const xmlChar *) name, (const xmlChar *) value); xmlXPathFreeObject (result); } } static void set_value(xmlXPathContextPtr context, const xmlChar *xpath, const xmlChar *value){ xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context); if ( result ) { xmlNodeSetPtr nodeset = result->nodesetval; xmlNodeSetContent(nodeset->nodeTab[0], value); xmlXPathFreeObject(result); } } static void get_request( char *buffer, int *len, const char *orderno, const char *username, const char *password, const char *imsi, const char *action, const char *serivcename) { xmlChar *buff; xmlDocPtr doc; if ( strcmp(action, "A") == 0 ) { doc = xmlParseFile("mobb_activate.xml"); } else if ( strcmp(action, "D") == 0 ) { doc = xmlParseFile("mobb_cancel.xml"); } else { doc = xmlParseFile("mobb_status.xml"); } if ( doc == NULL ) { printf("xmlParseFile failed\n"); return; } xmlXPathContextPtr context = xmlXPathNewContext(doc); if ( context == NULL ) { printf("xmlXPathNewContext failed\n"); return; } time_t now; struct tm ts; char timestamp[80]; time(&now); ts = *localtime(&now); strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S+08:00", &ts); set_prop(context, (const xmlChar *)"//ProvisioningRequest", (const xmlChar *) "TransactionId", (const xmlChar *) orderno); set_value(context, (const xmlChar *)"//Login", (const xmlChar *) username); set_value(context, (const xmlChar *)"//Password", (const xmlChar *) password); set_value(context, (const xmlChar *)"//TimeStamp", (const xmlChar *) timestamp); set_value(context, (const xmlChar *)"//ProvisioningDataItem[@name='IMSI']", (const xmlChar *) imsi); if ( strcmp(action, "A") == 0 ) { set_value(context, (const xmlChar *)"//ProvisioningDataItem[@name='ServiceName']", (const xmlChar *) serivcename); } xmlDocDumpMemory(doc, &buff, len); strcpy(buffer, (char *) buff); printf("%s\n", buffer); xmlFree(buff); xmlXPathFreeContext(context); xmlFreeDoc(doc); xmlCleanupParser(); } static size_t get_response(void *ptr, size_t size, size_t nmemb, void *stream) { char resp[2048]; strcpy(resp, (char *) ptr); if ( strncmp(resp, "<?xml", 5) == 0 ) { printf("%s\n", resp); xmlDocPtr doc = xmlParseMemory(resp, strlen(resp)); xmlXPathContextPtr context = xmlXPathNewContext(doc); xmlXPathObjectPtr result = xmlXPathEvalExpression((const xmlChar *) "//ErrorCode", context); if ( result ) { xmlNodeSetPtr nodeset = result->nodesetval; printf("MD_RT_CODE: %s\n", xmlNodeGetContent(nodeset->nodeTab[0])); xmlXPathFreeObject(result); } result = xmlXPathEvalExpression((const xmlChar *) "//ErrorDescription", context); if ( result ) { xmlNodeSetPtr nodeset = result->nodesetval; printf("MD_RT_MESSAGE: %s\n", xmlNodeGetContent(nodeset->nodeTab[0])); xmlXPathFreeObject(result); } xmlXPathFreeContext(context); xmlFreeDoc(doc); } return strlen(resp); } int main(int argc, char **argv) { if ( argc < 7 || argc > 8 ) { printf("Usage: BlackBerryProv endpoint username password orderno imsi action [servicename]\n"); exit(-1); } const char *action = argv[6]; if ( strcmp(action, "A") != 0 && strcmp(action, "D") != 0 && strcmp(action, "S") != 0 ) { printf("action must be A (Activation), D (De-activation) or S (Status)\n"); exit(-1); } if ( strcmp(action, "A") == 0 && argc == 7 ) { printf("service name is needed in activation\n"); exit(-1); } const char *endpoint = argv[1]; const char *username = argv[2]; const char *password = argv[3]; const char *orderno = argv[4]; const char *imsi = argv[5]; const char *servicename = argv[7]; char buffer[2048]; int len = 0; get_request(buffer, &len, orderno, username, password, imsi, action, servicename); struct curl_slist *headers = NULL; CURL *curl = curl_easy_init(); if ( curl ) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buffer); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(buffer)); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_HEADER, 1); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); if ( strcmp(action, "S") != 0 ) { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_response); } curl_easy_perform(curl); /* always cleanup */ curl_easy_cleanup(curl); curl_slist_free_all(headers); } return 0; }

这篇关于libcurl通过HTTPS方式提交XML并解析响应信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/799519

相关文章

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

虚拟机与物理机的文件共享方式

《虚拟机与物理机的文件共享方式》文章介绍了如何在KaliLinux虚拟机中实现物理机文件夹的直接挂载,以便在虚拟机中方便地读取和使用物理机上的文件,通过设置和配置,可以实现临时挂载和永久挂载,并提供... 目录虚拟机与物理机的文件共享1 虚拟机设置2 验证Kali下分享文件夹功能是否启用3 创建挂载目录4

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

详解Java如何向http/https接口发出请求

《详解Java如何向http/https接口发出请求》这篇文章主要为大家详细介绍了Java如何实现向http/https接口发出请求,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用Java发送web请求所用到的包都在java.net下,在具体使用时可以用如下代码,你可以把它封装成一

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W