收聽推送消息? (Listen to a push message?)


問題描述

收聽推送消息? (Listen to a push message?)

我必須無限期地收聽網絡服務的推送消息。只要內容有更新,我正在收聽的 Web 服務就會發送肥皂響應消息。收到消息後,我必須對其進行解析並將其存儲到結構中。以下是我的代碼。

CURL *curl;
CURLcode res;
const char *onlineWebServiceRequest = "<?xml version=\"1.0\" encoding=\"utf‑8\"?> <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema‑instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">  <soap:Body>    <GetCitiesByCountry xmlns=\"http://www.webserviceX.NET\">  <CountryName>Netherlands</CountryName>     </GetCitiesByCountry>    </soap:Body>    </soap:Envelope>";

if(curl){

    curl_easy_setopt(curl, CURLOPT_URL, "http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry" );

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, onlineWebServiceRequest);  

    res = curl_easy_perform(curl);       
    // call some other method to parse the res
    curl_easy_cleanup(curl);    
} 

疑問:接收推送消息的方式是否正確?如果是的話,

上面的代碼沒有檢查與web服務的連接狀態。我該如何檢查?

如果沒有,我可能對開源還有哪些其他選擇?

提前致謝


參考解法

方法 1:

You'd better off checking for errors.

To do that you have to enable CURLOPT_ERRORBUFFER option.

curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);

And add a buffer (it must be at least CURL_ERROR_SIZE bytes big).

char errbuf[CURL_ERROR_SIZE];

Additionally, you could set CURLOPT_FAILONERROR to true, to force curl to convert all response codes >= 300 to errors.

curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);

To get the actual output from the server you need to set a function to write the data (CURLOPT_WRITEFUNCTION) and a chunk of memory (CURLOPT_WRITEDATA).

A complete example adapted from [1] and [2]:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>

struct MemoryStruct {
  char *memory;
  size_t size;
};

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;

  mem‑>memory = (char *)realloc(mem‑>memory, mem‑>size + realsize + 1);
  if(mem‑>memory == NULL) {
    /* out of memory! */ 
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  memcpy(&(mem‑>memory[mem‑>size]), contents, realsize);
  mem‑>size += realsize;
  mem‑>memory[mem‑>size] = 0;

  return realsize;
}

int main(void)
{
  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */ 
  CURL curl = curl_easy_init();
  if(curl) {
    CURLcode res;

    struct MemoryStruct chunk;

    chunk.memory = (char *)malloc(1);  /* will be grown as needed by the realloc above */ 
    chunk.size = 0;    /* no data at this point */ 

    char errbuf[CURL_ERROR_SIZE];

    /* specify URL to get */ 
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");

    /* send all data to this function  */ 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

    /* we pass our 'chunk' struct to the callback function */ 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

    /* force curl to fail error when http code >= 300 */
    curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);

    /* provide a buffer to store errors in */
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);

    /* set the error buffer as empty before performing a request */
    errbuf[0] = 0;

    /* perform the request */
    res = curl_easy_perform(curl);

    /* if the request did not complete correctly, show the error
    information. if no detailed error information was written to errbuf
    show the more generic information from curl_easy_strerror instead.
    */
    if(res != CURLE_OK) {
      size_t len = strlen(errbuf);
      fprintf(stderr, "\nlibcurl: (%d) ", res);
      if(len)
        fprintf(stderr, "%s%s", errbuf,
                ((errbuf[len ‑ 1] != '\n') ? "\n" : ""));
      else
        fprintf(stderr, "%s\n", curl_easy_strerror(res));
    }
    else {
      /*
       * Now, our chunk.memory points to a memory block that is chunk.size
       * bytes big and contains the remote file.
       *
       * Do something nice with it!
       */ 

      printf("%lu bytes retrieved\n", (long)chunk.size);
    }

    /* cleanup curl stuff */ 
    curl_easy_cleanup(curl);

    free(chunk.memory);

  }

  /* we're done with libcurl, so clean it up */ 
  curl_global_cleanup();

  return 0;
}

(by Kidhlscalon)

參考文件

  1. Listen to a push message? (CC BY‑SA 2.5/3.0/4.0)

#curl #webservices-client #C++ #SOAP






相關問題

如何在所有 PayPal 交易中退還費用 (How to return fee in all PayPal transactions)

Cú pháp lệnh curl để đăng ký device_token trên Urban Airship là gì? (What is curl command syntax for device_token registration on Urban Airship?)

Nhận thời lượng video trên youtube từ URL với bash (Get youtube video duration from URL with bash)

đăng dữ liệu json với php curl cho đa phần / dữ liệu biểu mẫu để tải lên tệp nếu cakephp 2 (post json data with php curl for multipart/form-data for file upload vía cakephp 2)

ApacheMonitor 上的捲曲錯誤 (Curl error on ApacheMonitor)

從 ASP.net 使用 PHP 中的 POST 數據抓取數據 (Scraping data with POST data in PHP from ASP.net)

收聽推送消息? (Listen to a push message?)

在 PHP 中使用 cURL 的 RAW POST (RAW POST using cURL in PHP)

使用 php 和 curl 更新 mediawiki (using php and curl to update mediawiki)

在 Makefile 中鏈接 cURL (Linking cURL in Makefile)

將我的 .php 輸出發送到 curl 命令,該命令使用機器人在電報上向我發送消息 (Send my .php output to a curl command that messages me on telegram using a bot)

使用 Google My Business API 和 PHP 更新/修補本地帖子 (Updating/Patching a Local Post with Google My Business API and PHP)







留言討論