cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Help - retriveal of header & body info

From: Arunachalam, Sasikanth <Sasikanth.Arunachalam_at_geometricglobal.com>
Date: Mon, 4 Jan 2010 12:12:22 +0530

Hi,
 I have removed the line and used the following lines of code with
libcurl version 7.19.7 on AIX 5.3 OS

        CURL *curl_handle;

      static const char *headerfilename = "head.out";

      FILE *headerfile;

      static const char *bodyfilename = "body.out";

      FILE *bodyfile;

 

      curl_global_init(CURL_GLOBAL_ALL);

       curl_handle = curl_easy_init();

 

      CURLcode retCode = CURLE_OK ;

      const char *pErrMsg = NULL;

      /* Setting Proxy*/

      retCode = curl_easy_setopt(curl_handle, CURLOPT_PROXY, "");

      /* set URL to get */

      retcode = curl_easy_setopt(curl_handle, CURLOPT_URL,
"http://m.in.yahoo.com/?p=us");

      /* no progress meter please */

      retCode = curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

      
      /* open the files */

      //headerfile = fopen(headerfilename,"w");

      headerfile = fopen("/homes/head.out","w");

      if (headerfile == NULL)

      {

            curl_easy_cleanup(curl_handle);

            return -1;

      }

      bodyfile = fopen("/homes/ body.out","w");

      if (bodyfile == NULL)

      {

            curl_easy_cleanup(curl_handle);

            return -1; }

 

      /* we want the headers to this file handle */

      retCode = curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER,
headerfile);

      retCode = curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA,
(FILE*)bodyfile);

      /* get it! */

      retCode = curl_easy_perform(curl_handle);

      /* close the header file */

      fclose(headerfile);

 

      /* cleanup curl stuff */

      curl_easy_cleanup(curl_handle);

Still I am not getting the information in body.out file.

I could see that the stream is getting retrieved inside write_data
function but its not written to the file.

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
  char* cptr = (char*)ptr;
  cout << cptr << endl;
  cout << cptr << endl;
  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

Please let me know if you can give me some inputs why this information
is not getting written into the file stream in UNIX.
 

Br
Sasikanth
----------------------------------------------------------------------

Message: 1
Date: Thu, 31 Dec 2009 17:11:27 +0530
From: "Arunachalam, Sasikanth"
        <Sasikanth.Arunachalam_at_geometricglobal.com>
To: <curl-library_at_cool.haxx.se>
Subject: Help - retriveal of header & body info
Message-ID:
        <DF618EB109E4DD40BC9269E86026E3E12C5C7A_at_RUBY1.geometric.local>
Content-Type: text/plain; charset="us-ascii"

Hi,

      I am using libcurl version 7.19.7 . I have tried the following
piece of code to retrieve the header & body information of the a website
with Mozila firebox 5.0 in AIX 5.3 OS

 

      CURL *curl_handle;

      static const char *headerfilename = "head.out";

      FILE *headerfile;

      static const char *bodyfilename = "body.out";

      FILE *bodyfile;

 

      curl_global_init(CURL_GLOBAL_ALL);

 

      /* init the curl session */

      curl_handle = curl_easy_init();

 

      CURLcode retCode = CURLE_OK ;

      const char *pErrMsg = NULL;

      /* Setting Proxy*/

      retCode = curl_easy_setopt(curl_handle, CURLOPT_PROXY, "");

      

      pErrMsg = curl_easy_strerror(retCode);

      

      /* set URL to get */

      retcode = curl_easy_setopt(curl_handle, CURLOPT_URL,
"http://m.in.yahoo.com/?p=us");

      pErrMsg = curl_easy_strerror(retCode);

      

      /* no progress meter please */

      retCode = curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

 

      pErrMsg = curl_easy_strerror(retCode);

      

      /* send all data to this function */

      retCode =curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
write_data);

      

      pErrMsg = curl_easy_strerror(retCode);

      

      /* open the files */

      //headerfile = fopen(headerfilename,"w");

      headerfile = fopen("/homes/head.out","w");

      if (headerfile == NULL)

      {

            curl_easy_cleanup(curl_handle);

            return -1;

      }

      bodyfile = fopen("/homes/ body.out","w");

      if (bodyfile == NULL)

      {

            curl_easy_cleanup(curl_handle);

            return -1; }

 

      /* we want the headers to this file handle */

      retCode = curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER,
headerfile);

 

      pErrMsg = curl_easy_strerror(retCode);

      

      /*

      * Notice here that if you want the actual data sent anywhere else
but

      * stdout, you should consider using the CURLOPT_WRITEDATA option.
*/

 

 

      retCode = curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA,
bodyfile);

      pErrMsg = curl_easy_strerror(retCode);

      

 

      /* get it! */

      retCode = curl_easy_perform(curl_handle);

 

      pErrMsg = curl_easy_strerror(retCode);

      

      /* close the header file */

      fclose(headerfile);

 

      /* cleanup curl stuff */

      curl_easy_cleanup(curl_handle);

 

I am not able to retrieve the header & body information using the above
piece of code. I didn't get any error message using CURLcode.

 

I got the content of head.out as

 

HTTP/1.1 200 OK

content-language: en-US

content-type: text/html;charset=ISO-8859-1

date: Thu, 31 Dec 2009 11:27:15 GMT

p3p: CP="NON CUR OTPi OUR NOR UNI"

server: IBM_HTTP_Server

x-old-content-length: 1041

transfer-encoding: chunked

cache-control: no-cache

pragma: no-cache

expires: Thu, 01 Jan 1970 00:00:00 GMT

Set-Cookie: AMWEBJCT!%2FAppl!JSESSIONID=0000XIgzYlq7yx3qKdAUaCxtfWN:-1;
Path=/

Set-Cookie: PD_STATEFUL_a705f792-72e2-11db-b0e3-000629c30555=%2FAppl;
Path=/

 

& the empty information in body.out file.

 

On the other hand, I have performed the same operation in Windows with
Internet explorer and I am able to retrieve the header and body
information without any issues.

 

Request you to please let me know whether I need to set any specific
settings for my Unix development.

 

Br

Sasikanth

-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://cool.haxx.se/pipermail/curl-library/attachments/20091231/f3424d8
7/attachment.htm>

------------------------------

Message: 2
Date: Thu, 31 Dec 2009 13:26:18 +0100
From: Julien Chaffraix <julien.chaffraix_at_gmail.com>
To: libcurl development <curl-library_at_cool.haxx.se>
Subject: Re: Help - retriveal of header & body info
Message-ID:
        <4b6072ae0912310426p636a3cf5kb49d515edcda3819_at_mail.gmail.com>
Content-Type: text/plain; charset=windows-1252

Hi,

> ????? retCode =curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
> write_data);

You are overriding the default write function here.

> I am not able to retrieve the header & body information using the
above
> piece of code. I didn?t get any error message using CURLcode.
>
> I got the content of head.out as

[snipped the header information]

> & the? empty information in body.out file.

Do you get something in body.out if you remove the above line in your
code?

Best regards,
Julien

------------------------------

_______________________________________________
curl-library mailing list
curl-library_at_cool.haxx.se
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-library

End of curl-library Digest, Vol 52, Issue 59
********************************************
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-01-04