cURL / Mailing Lists / curl-library / Single Mail

curl-library

reagrding error in getting complete server response

From: Sunil Chandrasekharan <sunil.kainat_at_gmail.com>
Date: Fri, 25 Oct 2013 16:29:35 +0900

Hi All,

I am using libcurl for perfirming HTTP request and response to my webpage.
But when getting response, i am not getting the complete server response.
the response lines get truncated at one point ..and then nothing happens.

e.g

.....
10-25 15:53:22.264: XXX(14847): Security.8021x:true
10-25 15:53:22.264: XXX(14847): Event.AlarmInput:true
10-25 15:53:22.264: XXX(14847): Event.AlarmInput.Notification.HTTP.CGI:true
10-25 15:53:22.264: XXX(14847):
Event.AlarmInput.Notification.HTTP.CGI.SingleSession:true
10-25 15:53:22.264: XXX(14847): Event.AlarmInpu

There are around 10 more lines in the response, but my current log shows
that after "Event.AlarmInpu" there is no output.

hence i feel the server response data coming is not written properly inside
my curl function. Please support to rectify this issue and get the complete
server response.
Please find my code :

=============================

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;
  if (buffer && page->data && (page->len + len < (16 * 8192)) ) {
    memcpy(&page->data[page->len], buffer, len);
    page->len += len;
  }
  return len;
}

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

{
  pageInfo_t page;
  CURL *curl;
  CURLcode res;
  char *buffer;
  long response_code = 0;
  const jbyte *webpage;
   char *hostname, *username, *password , *userpswd;

  webpage = (*env)->GetStringUTFChars(env, webpageJStr, NULL);
  hostname = (*env)->GetStringUTFChars(env, host, NULL);
  username = (*env)->GetStringUTFChars(env, userid, NULL);
  password = (*env)->GetStringUTFChars(env, pass, NULL);

  userpswd = (char *)malloc(1024);
  sprintf(userpswd, "%s:%s", username, password);

  if (webpage == NULL)
  {
      return NULL; /* OutOfMemoryError already thrown */
  }
int memorysize = 8192;

  page.data = (char *)malloc(16 * memorysize);
  page.len = 0;
  if (page.data)
    memset(page.data, 32, 16 * memorysize);

  buffer = (char *)malloc(memorysize);

  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_USERPWD, userpswd);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    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);
        /* always cleanup */
    curl_easy_cleanup(curl);
if(response_code == 200)
     {
  page.data[page.len < memorysize ? page.len : memorysize-1] = '\0';
         sprintf(buffer, "%ld:%s",response_code, page.data);
         return (*env)->NewStringUTF(env, buffer);
 }
}
}

Here i am writing the response of the server to HTTPData function. You can
see that i have hardcoded my buffer variable memory alloocation memory size
as 8192 ..but still i am not getting the entire server response.

Please help in rectifying the problem.

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