我有一个调用java web服务的C程序,我正在使用curl库,我的问题是当代码执行行时
curl_easy_cleanup(curl);
程序返回错误:没有源可用于Curl_expire(),我的主要方法是llamadaServicio
这是我的C代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "frozen.h"
#include "TransaccionGrupoControl.h"
struct string {
char *ptr;
size_t len;
};
int key=0xF;
void encrypt(char string[]);
void decrypt(char string[]);
void init_string(struct string *s){
s->len = 0;
s->ptr = malloc(s->len+1);
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
s->ptr[0] = '\0';
}
size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
const char * llamadaServicio(char * entrada) {
char dest[]="\x00";
CURL *curl;
CURLcode res;
//struct json_token *arr, *arr2, *tok, *tok2;
struct curl_slist *list = NULL;
char str[100];
char in[]="entrada: ";
strcpy(str,entrada);
encrypt(str);
strcat(in,str);
list=curl_slist_append(list,in);
struct string s;
curl = curl_easy_init();
if(curl) {
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/WSEnlaceGrupoControl/service/enlace/enlaceTransaccion");
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 50000);
//curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
//curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(data));
//curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
if(CURLE_OK == res){
decrypt(s.ptr);
strcpy(dest,s.ptr);
free(s.ptr);
} else if(CURLE_OPERATION_TIMEDOUT == res) {
strncpy(dest, "28|Error de conexion.\x00", strlen("28|Error de conexion.\x00"));
} else if(CURLE_HTTP_POST_ERROR == res) {
strncpy(dest, "28|Error de conexion.\x00", strlen("28|Error de conexion.\x00"));
}
curl_easy_cleanup(curl);
}
return dest;
}
void encrypt(char string[]){
unsigned int i;
for(i=0;i<strlen(string);++i){
string[i] = string[i] - key;
}
}
void decrypt(char string[]){
unsigned int i;
for(i=0;i<strlen(string);++i){
string[i] = string[i] + key;
}
}
提前感谢,