-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
242 lines (196 loc) · 6.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.c
* Author: eduardo.j.aguilera
*
* Created on 23 de junio de 2016, 8:12
*/
#include <stdio.h>
#include <stdlib.h>
// Librerías incluidas
#include "soapH.h"
#include "httpget.h"
#include "httppost.h"
#include "json_c.h"
/*
*
*/
int main(int argc, char** argv) {
char *proxyHost = NULL;
if (argc == 2)
{
printf("Se ha introducido el proxy %s", argv[1]);
proxyHost = argv[1];
}
int rc = llamar_servicio_get(proxyHost);
rc = llamar_servicio_post(proxyHost);
return (EXIT_SUCCESS);
}
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;
}
int llamar_servicio_post(char *proxyHost)
{
struct value resJSON;
struct soap *soap = soap_new();
struct value jsonRequest;
struct _struct estructuraPoC;
struct value title, body, userId;
struct member miembrosPoC[3];
if (proxyHost && strlen(proxyHost) > 0) soap->proxy_host = proxyHost;
printf("Ejemplo de llamada get recuperando JSON\n");
printf("---------------------------------------\n");
if (soap_post_connect(soap, "http://jsonplaceholder.typicode.com/posts", NULL, "application/json"))
{
soap_print_fault(soap, stderr);
printf("Error en la conexión con la API post.\n");
goto finalizar;
}
// JSON con 3 miembros
// Title
title.__type = SOAP_TYPE__string;
char sTitle[10] = "foo";
title.ref = sTitle;
// body
body.__type = SOAP_TYPE__string;
char sBody[10] = "bar";
body.ref = sBody;
// userId
userId.__type = SOAP_TYPE__int;
int iUserId = 1;
userId.ref = &iUserId;
miembrosPoC[0].name = "title";
miembrosPoC[0].value = title;
miembrosPoC[1].name = "body";
miembrosPoC[1].value = body;
miembrosPoC[2].name = "userId";
miembrosPoC[2].value = userId;
estructuraPoC.__size = 3;
estructuraPoC.member = miembrosPoC;
jsonRequest.__type = SOAP_TYPE__struct;
jsonRequest.ref = &estructuraPoC;
if (json_send(soap, &jsonRequest))
{
soap_print_fault(soap, stderr);
printf("Error en el envío JSON en la llamada post.\n");
goto finalizar;
}
if (soap_end_send(soap))
{
soap_print_fault(soap, stderr);
printf("Error al finalizar envío JSON en la llamada post.");
goto finalizar;
}
if (soap_begin_recv(soap))
{
soap_print_fault(soap, stderr);
printf("Error en el inicio de la recepción JSON en la API post.");
goto finalizar;
}
if (json_recv(soap, &resJSON))
{
soap_print_fault(soap, stderr);
printf("Error en la obtención del JSON en la API post.\n");
goto finalizar;
}
if (soap_end_recv(soap))
{
soap_print_fault(soap, stderr);
printf("Error en la finalización de la API post.\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("Cadena: %s\n", (estructuraJSON->member + i)->value.ref);
}
else if ((estructuraJSON->member + i)->value.__type == SOAP_TYPE__int)
{
printf("Entero: %ld\n", *(int*)((estructuraJSON->member + i)->value.ref));
}
}
}
else
{
printf("Se recibe un JSON no esperado.\n");
goto finalizar;
}
printf("Ha finalizado correctamente la llamada post\n");
printf("------------------------------------------\n");
finalizar:
soap_destroy(soap);
soap_end(soap);
soap_free(soap);
return 0;
}