curl-library
Re: reagrding error in getting complete server response
Date: Mon, 18 Nov 2013 13:43:12 +0900
Hi All,
Kindly find my complete code
=================
struct pageInfo_t {
char *data;
size_t len;
};
static size_t HTTPData(void *buffer, size_t size, size_t nmemb, void
*userData)
{
size_t length = (size * nmemb);
struct pageInfo_t *page = (struct pageInfo_t *)userData;
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n data size=%ld
nmemb=%ld ====== \n",size,nmemb);
page->data = realloc(page->data,page->len + length +1);
if(page->data == NULL)
{
// out of memory!
return 0;
}
memcpy(&page->data[page->len], buffer, length);
page->len += length;
page->data[page->len] = 0;
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n Need to print
size : %d ====== \n", length);
return length;
}
char *buffer;
struct pageInfo_t page;
jstring Java_com_samsung_jnitest_MainActivity_JNIGetWebpage( JNIEnv* env,
jobject entryObject, jstring webpageJStr)
{
CURL *curl;
CURLcode res;
long response_code = 0;
double content_length = 0;
const jbyte *webpage;
char *hostname, *username, *password;
webpage = (*env)->GetStringUTFChars(env, webpageJStr, NULL);
if (webpage == NULL) {
return NULL; /* OutOfMemoryError already thrown */
}
page.data = (char *)malloc(4096);
page.len = 0;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, webpage);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 100);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 50);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&page);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&content_length);
if(res != CURLE_OK)
{
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "curl_easy_perform()
failed: %s\n", curl_easy_strerror(res));
return (*env)->NewStringUTF(env,curl_easy_strerror(res));
}
else
{
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:content
length(%d)======\n", strlen(page.data));
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:page
size(%ld)======\n", content_length);
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:%s
======\n", page.data);
buffer = (char *)malloc(2 * strlen(page.data));
sprintf(buffer, "%ld:%s \n",response_code, page.data);
}
/* always cleanup */
(*env)->ReleaseStringUTFChars(env, webpageJStr, webpage);
curl_easy_cleanup(curl);
if(page.data){
free(page.data);
}
return (*env)->NewStringUTF(env,buffer);
}
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-11-18