int main(int argc, char **argv) { struct soap soap; if (argc < 2) { fprintf(stderr, "Usage: httpgettest URL\n"); exit(0); } soap_init(&soap); soap_register_plugin(&soap, http_get); // register plugin if (soap_get_connect(&soap, argv[1], NULL) || soap_begin_recv(&soap)) { soap_print_fault(&soap, stderr); exit(1); } if (soap.http_content) printf("Content type = %s\n", soap.http_content); printf("Content length = %ld\n", soap.length); if ((soap.mode & SOAP_IO) == SOAP_IO_CHUNK #ifdef WITH_ZLIB || soap.zlib_in != SOAP_ZLIB_NONE #endif ) { soap_wchar c; // This loop handles chunked/compressed transfers for (;;) { if ((c = soap_getchar(&soap)) == (int)EOF) break; putchar((int)c); } } else { // This loop handles HTTP transfers (with HTTP content length set) if (soap.length) { size_t i; for (i = soap.length; i; i--) { soap_wchar c; if ((c = soap_getchar(&soap)) == (int)EOF) { soap.error = SOAP_EOF; break; } putchar((int)c); } } } soap_end_recv(&soap); soap_end(&soap); soap_done(&soap); return 0; }
int llamar_servicio_get(char *proxyHost) { struct value resJSON; struct soap *soap = soap_new(); soap_register_plugin(soap, http_get); if (proxyHost && strlen(proxyHost) > 0) soap->proxy_host = proxyHost; printf("Ejemplo de llamada get recuperando JSON\n"); printf("---------------------------------------\n"); if (soap_get_connect(soap, "http://jsonplaceholder.typicode.com/posts/1", NULL)) { soap_print_fault(soap, stderr); printf("Error en la conexión con la API get.\n"); goto finalizar; } if (soap_begin_recv(soap)) { soap_print_fault(soap, stderr); printf("Error en la llamada a la API get.\n"); goto finalizar; } if (json_recv(soap, &resJSON)) { soap_print_fault(soap, stderr); printf("Error en la obtención del JSON en la API get.\n"); goto finalizar; } if (soap_end_recv(soap)) { soap_print_fault(soap, stderr); printf("Error en la finalización de la API get.\n"); goto finalizar; } if (resJSON.__type == SOAP_TYPE__struct) { struct _struct *estructuraJSON; estructuraJSON = (struct _struct *)resJSON.ref; int i; for (i=0; i < estructuraJSON->__size; i++) { printf("%s: ", (estructuraJSON->member + i)->name); if ((estructuraJSON->member + i)->value.__type == SOAP_TYPE__string) { printf("%s\n", (estructuraJSON->member + i)->value.ref); } else if ((estructuraJSON->member + i)->value.__type == SOAP_TYPE__int) { printf("%ld\n", (estructuraJSON->member + i)->value.ref); } } } else { printf("Se recibe un JSON no esperado.\n"); goto finalizar; } printf("Ha finalizado correctamente la llamada get\n"); printf("------------------------------------------\n"); finalizar: soap_destroy(soap); soap_end(soap); soap_free(soap); return 0; }