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; }

你可能感兴趣的:(xml,struct,ssl,null,buffer,action)