我有一个程序使用cURL通过http将一些xml数据发布到云端。就在执行curl_easy_perform()之前,当我将数据打印到看起来没问题的控制台时,但是在接收端,xml已损坏,要么被截断,要么被破坏。我无法找出原因,因为问题是间歇性的
发送的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><plugin><macAddress>08863B6CAEF8</macAddress><serialNumber>221212K01008A4</serialNumber><friendlyName>WeMo Switch</friendlyName><udnName>uuid:Socket-1_0-221212K01008A4</udnName><homeId>700511319</homeId><deviceType>Switch</deviceType><status>0</status><statusTS>1425892983</statusTS><firmwareVersion>WeMo_WW_2.00.8337.DVT-OWRT-SNS</firmwareVersion><fwUpgradeStatus>4</fwUpgradeStatus><signalStrength>96</signalStrength><attributeLists action="notify"><attribute><name>RuleAutoOffTime</name><value>0</value></attribute></attributeLists></plugin>
在接收方,我得到了这个
tatusTS>1423077095</statusTS><firmwareVersion>WeMo_WW_2.00.7284.PVT</firmwareVersion><fwUpgradeStatus>4</fwUpgradeStatus><signalStrength>100</signalStrength><eventDuration>123</eventDuration><startTime>1378302633</startTime></plugin>
以下是执行上述任务的代码段
//Actual data being sent is inData[] array in *pUsrAppData
// user Application data
typedef struct userAppData {
char url[SIZE_256B]; // url to which the session needs to be established
KeyValue keyVal[SIZE_32B]; // html content heaer specifying the key and value
int keyValLen; //Number of pairs in keyValue
char inData[DATA_BUF_LEN]; // file path or data which needs to be sent
int inDataLength; // length of data which needs to be sent, if <=0 then indata contains file path
// or if it is >0 then it should contain length of data in indata
int inDataCount; /**This is added for thread access problem 400 and 500 bad request*/
char *outData; // pointer to the data which is received in response from the server
//char outData[DATA_BUF_LEN]; // pointer to the data which is received in response from the server
int outDataLength; // data length of the out data
char outHeader[DATA_BUF_LEN]; // pointer to the header data which is received in response from the server
int outHeaderLength; // data length of the out header data
int httpsFlag; //Flag to identify https or http, should be set to 1 for https and 0 for http
int disableFlag; //Flag to identify whether error handling is to be enabled or disabled
int partNumber; //Specifies the current file part
char eTag[SIZE_4B][SIZE_128B]; // eTag for file uploads
char mac[KEY_BUF_LEN]; //max address used internally for posting transaction info for PUT case
char cookie_data[KEY_VAL_LEN];
int outResp; //Response value in outHeader
int nStatusCode; //HTTP Status Code
}UserAppData;
// user session data
typedef struct userAppSessionData {
int sessionId; // id corresponding to the session created
CURL * curl; // pointer to curl SessionHandle structure
}UserAppSessionData;
typedef struct webSessionListNode{
UserAppData *pUsrAppData;
UserAppSessionData *pUsrAppSsnData;
}WebSessionListNode;
WebSessionListNode *pWebSsnListNode = NULL;
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_URL, pWebSsnListNode->pUsrAppData->url);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_HTTPHEADER, slist);
/* specify we want to POST data */
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_POST, 1L);
/* Write and header callback */
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_WRITEHEADER, pWebSsnListNode->pUsrAppSsnData);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_WRITEDATA, pWebSsnListNode->pUsrAppSsnData);
/* verbose debug output option */
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_READFUNCTION, read_callback);
/* pointer to pass to our read function */
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_READDATA, pWebSsnListNode->pUsrAppData);
/* Set the expected POST size. */
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_POSTFIELDSIZE, (long)(pUsrAppData->inDataLength));
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_NOSIGNAL,1L);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_CONNECTTIMEOUT, 60L);/* 60 seconds - timeout when connecting to web server */
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_TIMEOUT, 60L);/* 60 seconds - read timeout */
res = curl_easy_perform(pWebSsnListNode->pUsrAppSsnData->curl);
//Here, xml being print is always sane
APP_LOG("HTTPSWRAPPER", LOG_DEBUG, "Sent XML is : %s\n", pUsrAppData->inData);
我使用的cURL版本是7.29,我用来存储数据的缓冲区足够长。
答案 0 :(得分:0)
由于(显然)您的POST数据是一个简单的\0
终止字符串而不是流,因此您可以使用以下内容:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, pUsrAppData->inData);
而不是对:
的调用curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_READDATA, pWebSsnListNode->pUsrAppData);
curl_easy_setopt(pWebSsnListNode->pUsrAppSsnData->curl, CURLOPT_POSTFIELDSIZE, (long)(pUsrAppData->inDataLength));
此外,根据接收方的不同,您可能需要添加一个显式调用:
curl_easy_setopt(CURL *handle, CURLOPT_HTTPHEADER, struct curl_slist *headers);
将Content-Type
标头设置为application/xml
。