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

Xml cURL-在接收器上获取部分数据(400错误请求),xml,http,post,curl,Xml,Http,Post,Curl,我有一个程序,使用cURL over http将一些xml数据发布到云。就在执行curl\u easy\u-perform之前,当我将数据打印到控制台时,看起来还可以,但在接收端,xml已损坏,或者被截断或损坏。我无法找出原因,因为问题是间歇性的 已发送XML: 08863B6CAEF82212K01008A4WEMO开关UUID:Socket-1_0-221212K01008A4700511319开关01425892983WEMO_WW_2.00.8337.DVT-OWRT-SNS496规则自

我有一个程序,使用cURL over http将一些xml数据发布到云。就在执行curl\u easy\u-perform之前,当我将数据打印到控制台时,看起来还可以,但在接收端,xml已损坏,或者被截断或损坏。我无法找出原因,因为问题是间歇性的

已发送XML:

08863B6CAEF82212K01008A4WEMO开关UUID:Socket-1_0-221212K01008A4700511319开关01425892983WEMO_WW_2.00.8337.DVT-OWRT-SNS496规则自动关闭时间0 在接收端,我得到了这个

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

要将内容类型标题设置为application/xml之类的内容。

您应该发布实际发送数据的代码。@cosmo0已添加…感谢您提醒我,如果我可以问的话,为什么这个标签是PHP?看起来更像C,不是吗?也许接收端的代码丢失了?这就是PHP?此外,read_回调也不是问题的一部分,这很可能是问题的原因。这将只是做手术后的另一种选择。。。正当或者它会在一定程度上影响数据传输。这是一种简单的POST替代方案;这不会影响数据传输,但我猜您的read_回调是不正确的,这样可以避免调用routineI将测试耐久性设置的更改…如果有效,将接受您的答案。