色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

c 獲取http json數(shù)據(jù)

林玟書1年前7瀏覽0評論

在C語言編程中,獲取HTTP JSON數(shù)據(jù)是一個常見的需求。下面我們介紹一些方法:

1. cURL庫

#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
char *url = "http://example.com/data.json";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}

2. HTTP庫

#include <stdio.h>
#include <http.h>
int main(void)
{
struct http_get_options options = {
.host = "example.com",
.port = "80",
.path = "/data.json",
.headers = "Connection: close\r\n",
};
struct http_response *response = http_get(options);
printf("%.*s", (int) response->body_size, response->body);
http_response_destroy(response);
return 0;
}

3. libmicrohttpd庫

#include <stdlib.h>
#include <microhttpd.h>
#include <jansson.h>
static int answer_to_connection (void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
const char *json_response = "{\"name\": \"John\", \"age\": 30}";
struct MHD_Response *response;
int ret;
response = MHD_create_response_from_buffer(strlen(json_response),
(void*) json_response,
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int main ()
{
struct MHD_Daemon *daemon;
daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, 8080, NULL, NULL,
&answer_to_connection, NULL, MHD_OPTION_END);
if (NULL == daemon) return 1;
(void) getchar ();
MHD_stop_daemon (daemon);
return 0;
}

以上就是一些獲取HTTP JSON數(shù)據(jù)的方法,希望對您有幫助。