cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: reagrding error in getting complete server response

From: Sunil Chandrasekharan <sunil.kainat_at_gmail.com>
Date: Thu, 14 Nov 2013 13:36:17 +0900

Hi ,

I didnt intent to say that your code has issues.
i know its something to do with my set up.

I am working on an android platform and i am using curl library for http
request.

from my android activity i call this method:

=== String set = JNIGetWebpage(url,host,port,userid,password); ==== (
the url is the sample.pl file inside the apache server that i attached in
my last post)

it takes to the curltest.c file
this is my curltest.c file :

======

* #include <string.h> #include <jni.h> #include <stdio.h> #include
<stdlib.h>*

* #include* "curl/curl.h"

* #include <android/log.h>*

 * typedef* *struct* pageInfo_t {

*char* *data;

*int* len;

} pageInfo_t;

* static* size_t *HTTPData*(*void* *buffer, size_t size, size_t nmemb,
*void* *userData) {

*int* len = size * nmemb;

pageInfo_t *page = (pageInfo_t *)userData;

page->data = realloc(page->data,page->len + len +1);

*if*(page->data == NULL)

 {

*return* 0;

}

memcpy(&page->data[page->len], buffer, len);

page->len += len;

page->data[page->len] = 0;

__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n print size : %d
====== \n", len);

__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n print data : %s
====== \n", page->data);

*return* len;

}

pageInfo_t page;

// *Interface* *funciton* that will *recieve* web page *fom* Java

jstring *Java_com_samsung_jnitest_MainActivity_JNIGetWebpage*( JNIEnv* env,
jobject entryObject, jstring webpageJStr,jstring host, jint port ,jstring
userid, jstring pass)

{

CURL *curl;

CURLcode res;

*char* *buffer;

*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 = malloc(4096);

page.len = 0;

page.data[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, &page);

res = curl_easy_perform(curl);

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &content_length);

/* always cleanup */

curl_easy_cleanup(curl);

(*env)->ReleaseStringUTFChars(env, webpageJStr, webpage);

*if*(res == 0) {

__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n Need to print :
content length(%d) %d %s ======\n", strlen(page.data),page.len, page.data);

}

*return* (*env)->NewStringUTF(env,page.data);
}

The output that i expect is around 100 lines , but i am getting around 60
lines only ..( just an e,g)
i see that the size_t method is called 3 times . i have print the logs to
see what is happening inside
it enters the fucntion 3 times and log shows size and data . i see the size
varies 3 times and total it shows 11817 as the size...
But when i return back "page.data" to my application it still shows 11817
as size
the output is much more ...so even though size_t method is called 3
times..the amount of response data written is still not complete...
hence the output is truncated output...

1. i dont know why the complete response is not getting retrieved
2. the size_t method is called 3 times..but the data is still
incomplete...not sure why

On Thu, Nov 14, 2013 at 11:56 AM, Daniel Stenberg <daniel_at_haxx.se> wrote:

> On Thu, 14 Nov 2013, Sunil Chandrasekharan wrote:
>
> Please stop the top-posting.
>
>
> Can you add my sample.pl file inside apache. then start apache and try to
>> access it using browser . if it displays the entire ouput. then fine. now
>> use the example c file as given by Daniel http://curl.haxx.se/libcurl/c/
>> getinmemory.html
>>
>> Please let me know if you are able to get the same output with the c file
>> as it is shown using browser.
>>
>> I just want to clarify here that the same c file i have used and still i
>> am not getting the entire response lines as output.
>>
>
> Exactly how does it misbehave? You're not very specific here. Can you also
> show us what headers your server sends back in that response?
>
> That example has been working fine for everyone for many years, and if it
> doesn't work there's a bug in libcurl and it would then hit a large amount
> of people so it isn't likely that something like that would go on
> unnoticed. I think the problem is rather somewhere in your test setup or
> something...
>
>
> --
>
> / daniel.haxx.se
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-11-14