gpt4 book ai didi

xml - cURL - 在接收器上获取部分数据(400 错误请求)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:29:23 25 4
gpt4 key购买 nike

我有一个程序可以使用 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,我用来存储数据的缓冲区有足够的长度。

最佳答案

因为(显然)您的 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 header 设置为类似 application/xml 的内容。

关于xml - cURL - 在接收器上获取部分数据(400 错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28939957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com