libcurl通过HTTPS方式提交XML并解析响应信息
2011-02-10 16:10
459 查看
不用太多解释,需要的自然有用。稍微有一丝难度的是某个地方用到回调函数,关于回调函数的概念,请百度。
程序中用到XPath,不了解的可以看这里:
http://www.w3school.com.cn/xpath/index.asp
还有这里:
http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html
程序中用到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; }
相关文章推荐
- PHP 以POST方式提交XML、获取XML,最后解析XML
- 原始ajax通过xmlHttpRequest对象的send()方法提交数据--Get方式和Post方式
- PHP 以POST方式提交XML、获取XML,最后解析XML
- spring通过xml方式配置AOP时,提示错误信息:匹配的通配符是严格的,但是对于元素'aop:aspectj- 自动代理”。
- Spring如何通过application.xml解析出bean的配置信息
- java 解析xml文档---通过XmlPullParser解析方式
- php获取通过http协议post提交过来xml数据及解析xml
- 通过AJAX的JS、JQuery两种方式解析XML示例介绍
- SpringMVC 通过XML方式进行配置解析BeanNameUrlHandlerMapping
- iOS开发工具-如何使用网络封包分析工具Charles,通过配置proxy对http、https、tcp、udp 等协议的请求响应过程交互信息进行分析、判断、解决我们移动开发中的遇到的各种实际问题。
- Python通过DOM和SAX方式解析XML的应用实例分享
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- 创建一个book.xml文档,PHP通过DOM方式解析获得每本书的作者和书名
- 通过递归的方式解析String 字符串类型的XML并保存进实体
- PHP 以POST方式提交XML、获取XML,最后解析XML
- Java 通过HttpClient 带证书方式请求Https协议网站 (Post方式带xml文档,Get以及Delete方式)
- 通过http/https的POST方式,发送和接受XML文件内容
- java通过post方式发送接收xml信息
- php获取通过http协议post提交过来xml数据及解析xml
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)