The attached sample may be enough to demonstrate what you want. But I am doing a post request instead of get, basically the same though.
Hope this help.
Kiyo

yeguo wu wrote:
Hi All
   I am a fresh man to use the libneon.
   but I don't know how to get the response to a string or char * simply.
In the source code , I just find the function dispatch_to_fd to read the response to a file or stdout, who can tell me how to code it?
   thanks
                                           best wishes
wuyeguo

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

_______________________________________________
neon mailing list
[email protected]
http://mailman.webdav.org/mailman/listinfo/neon

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ne_session.h"
#include "ne_uri.h"
#include "ne_basic.h"
#include "ne_utils.h"
#include "ne_request.h"

static char *
read_req(ne_request *req)
{
   ne_buffer *buf = ne_buffer_create();
   ssize_t len;
   char data[16384];
   while ((len = ne_read_response_block(req, data, sizeof data)) > 0)
      ne_buffer_append(buf, data, len);
   if (len == 0) return ne_buffer_finish(buf);
   ne_buffer_destroy(buf);
   return NULL;
}

static int
handle_req(ne_request *req, char** pdata)
{
   *pdata = NULL;
   int ret = NE_ERROR;
   const ne_status *const st = ne_get_status(req);
   do {
      if ((ret = ne_begin_request(req)) != NE_OK) break;
      if (st->klass == 2) {
         *pdata = read_req(req);
         ret = (*pdata != NULL) ? NE_OK : NE_ERROR;
      } else {
         ret = ne_discard_response(req);
      }
      if (ret == NE_OK) ret = ne_end_request(req);
   } while (ret == NE_RETRY);
   if (ret != NE_OK && *pdata != NULL) {
      ne_free(*pdata);
      *pdata = NULL;
   }
   return ret;
}

static char*
do_http_post(const char* url, const char* data, int timeout)
{
   char* res = NULL;
   ne_uri uri = { 0 };
   ne_session *sess = NULL;
   ne_request *req = NULL;
#if 0
   ne_uri uri2 = { 0 };
   char* path;
#endif
   int ret;
   if (ne_uri_parse(url, &uri) || uri.host == NULL || uri.path == NULL) goto lbl_exit;
   if (uri.scheme == NULL) uri.scheme = "http";
   if (uri.port == 0) uri.port = ne_uri_defaultport(uri.scheme);
   if ((sess = ne_session_create(uri.scheme, uri.host, uri.port)) == NULL) goto lbl_exit;
   if (timeout > 0) ne_set_read_timeout(sess, timeout);
#if 0 /* not work with 0.25.5 - ne_uri_unparse crashes */
   memcpy(&uri2, &uri, sizeof(uri));
   uri2.scheme = NULL;
   uri2.host = NULL;
   if ((path = ne_uri_unparse(&uri2)) == NULL) goto lbl_exit;
   req = ne_request_create(sess, "POST", path);
#else
   req = ne_request_create(sess, "POST", url);
#endif
#if 0
   ne_free(path);
#endif
   if (req == NULL) goto lbl_exit;
   ne_add_request_header(req, "Content-Type", "application/x-www-form-urlencoded");
#if 0 /* new from 0.26 */
   ne_set_request_flag(req, NE_REQFLAG_IDEMPOTENT, 0);
#endif
   if (data == NULL) data = "";
   ne_set_request_body_buffer(req, data, strlen(data));
   ret = handle_req(req, &res);
lbl_exit:
   if (req != NULL) ne_request_destroy(req);
   if (sess != NULL) ne_session_destroy(sess);
   ne_uri_free(&uri);
   return res;
}

int main(int argc, char* argv[])
{
   if (argc < 3) return 1;
   if (ne_sock_init()) return 2;
   char *res = do_http_post(argv[1], argv[2], 0);
   if (res == NULL)
      fprintf(stderr, "failed\n");
   else {
      printf("%s\n", res);
      ne_free(res);
   }
   ne_sock_exit();
   return 0;
}
_______________________________________________
neon mailing list
[email protected]
http://mailman.webdav.org/mailman/listinfo/neon

Reply via email to