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: Tue, 19 Nov 2013 10:48:47 +0900

Hi thanks for clarifying the issues..

1.
I am adding the updated new program but with the same problem of not
getting complete output
As checked the http server is not sending content length ...the content
length is shown as -1

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=%zu
nmemb=%zu
====== \n",size,nmemb);
 page->data = realloc(page->data,page->len + length +1);
  if(page->data == NULL)
  {

          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 : %zu ======
\n", length);
  return length;
}
jstring Java_com_samsung_jnitest_MainActivity_JNIGetWebpage( JNIEnv* env,
                                               jobject entryObject,
            jstring webpageJStr)
{
  CURL *curl;
  CURLcode res;
  auto struct pageInfo_t page;
  long response_code = 0;
  double content_length = 0;
  const jbyte *webpage;
  jstring r = NULL;
  char *hostname, *username, *password;
 webpage = (*env)->GetStringUTFChars(env, webpageJStr, NULL);

  if (webpage == NULL) {
      return NULL;
  }
    page.data = (char *)malloc(4096);
   page.len = 0;
   page.data[0] = '\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);

    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
    {
      curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
       curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&content_length);
      __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:page
data(%d)======
\n", strlen(page.data));
      __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:content
length(%lg)
======\n", content_length);
      __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:content
length(%zu)
======\n", page.len);
      __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:%s
======\n",
page.data);

    }

   }
     /* always cleanup */
  (*env)->ReleaseStringUTFChars(env, webpageJStr, webpage);
        curl_easy_cleanup(curl);
        r = (*env)->NewStringUTF(env,page.data);
        free(page.data);
        page.data = NULL;
        return r;
}
2.
this i cross checked with the curl program given in the link :
http://curl.haxx.se/libcurl/c/getinmemory.html

it is almost same...No Change at all now..

Kindly let me know if something wrong i am doing?

3.
This is the output that i want to get.. I am priniting the same lines
around 5 times to make a big ouput data. Then when i run the code i am
getting only 3 iterations of output... So as we discussed about a binary 0,
can you please tell me if there is some problem in the ouput lines?
Sample output :

COMSPEC="C:\Windows\system32\cmd.exe"
CONTEXT_DOCUMENT_ROOT="D:/xampp/cgi-bin/"
CONTEXT_PREFIX="/cgi-bin/"
DOCUMENT_ROOT="D:/xampp/htdocs"
GATEWAY_INTERFACE="CGI/1.1"
HTTP_ACCEPT="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
HTTP_ACCEPT_ENCODING="xxip,sdch"
HTTP_ACCEPT_LANGUAGE="en-US,en;q=0.8"
HTTP_CONNECTION="keep-alive"
HTTP_HOST="77.707.77.101"
HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
MIBDIRS="D:/xampp/php/extras/mibs"
MYSQL_HOME="\xampp\mysql\bin"
OPENSSL_CONF="D:/xampp/apache/bin/openssl.cnf"
PATH="C:\Program Files\Intel\iCLS
Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program
Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program
Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program
Files\Java\jdk1.6.0_45\bin;;C:\Program Files\Java\jre6\bin;C:\Program
Files\TortoiseSVN\bin;D:\Android-environment\swigwin-2.0.11\swigwin-2.0.11;D:\Android-environment\android-ndk-r9-windows-x86\android-ndk-r9;D:\Android-environment\adt-bundle-windows-x86-20130729\adt-bundle-windows-x86-20130729\sdk\platform-tools"
PATHEXT=".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
PHPRC="\xampp\php"
PHP_PEAR_SYSCONF_DIR="\xampp\php"
QUERY_STRING=""
REMOTE_ADDR="77.707.77.707"
REMOTE_PORT="711"
REQUEST_METHOD="GET"
REQUEST_SCHEME="http"
REQUEST_URI="/cgi-bin/printenv.pl"
SCRIPT_FILENAME="D:/xampp/cgi-bin/printenv.pl"
SCRIPT_NAME="/cgi-bin/printenv.pl"
SERVER_ADDR="77.707.77.707"
SERVER_ADMIN="postmaster_at_localhost"
SERVER_NAME="77.707.77.707"
SERVER_PORT="80"
SERVER_PROTOCOL="HTTP/1.1"
SERVER_SIGNATURE="<address>Apache/2.4.4 (Win32) OpenSSL/0.9.8y
PHP/5.4.19 Server at 77.707.77.707 Port 80</address>\n"
SERVER_SOFTWARE="Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19"
SYSTEMROOT="C:\Windows"
TMP="\xampp\tmp"
WINDIR="C:\Windows"

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