HTTP Client example

1,003 views
Skip to first unread message

Jordan Shelley

unread,
Aug 12, 2014, 8:52:34 PM8/12/14
to cive...@googlegroups.com
Are there any examples that show how to use civetweb as an embedded HTTP client? 

bel

unread,
Aug 13, 2014, 2:18:57 PM8/13/14
to cive...@googlegroups.com


On Wednesday, August 13, 2014 2:52:34 AM UTC+2, Jordan Shelley wrote:
Are there any examples that show how to use civetweb as an embedded HTTP client? 

You could use the mg_download function:

    /* Download data from the remote web server.
         host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
         port: port number, e.g. 80.
         use_ssl: wether to use SSL connection.
         error_buffer, error_buffer_size: error message placeholder.
         request_fmt,...: HTTP request.
       Return:
         On success, valid pointer to the new connection, suitable for mg_read().
         On error, NULL. error_buffer contains error message.
       Example:
         char ebuf[100];
         struct mg_connection *conn;
         conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
                            "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
     */

    CIVETWEB_API struct mg_connection *mg_download(const char *host, int port, int use_ssl,
                                                   char *error_buffer, size_t error_buffer_size,
                                                   PRINTF_FORMAT_STRING(const char *request_fmt),
                                                   ...) PRINTF_ARGS(6, 7);


Some examples can be found in  unit_test.c, function test_mg_download.


Jordan Shelley

unread,
Aug 14, 2014, 7:46:21 AM8/14/14
to cive...@googlegroups.com
Thank you! Does mg_download support persistent connections? I'm planning on implementing a websocket client interface for civetweb. Do you see any problem with doing that? Thanks.

bel

unread,
Aug 14, 2014, 6:31:02 PM8/14/14
to cive...@googlegroups.com

You wold have to use HTTP/1.1 instead of HTTP/1.0 in the mg_download request.
After you read the response with mg_read, you could use mg_write/mg_printf for a new request with the same connection.
I think websockets have to work this way as well, but I did not test it.


                 

Jordan Shelley

unread,
Aug 15, 2014, 7:59:21 AM8/15/14
to bel, cive...@googlegroups.com
Thanks again! Is this something you would be interested in including in civetweb? If it works, would you like me to create a pull request?


--
Sourceforge
https://sourceforge.net/projects/civetweb/
---
You received this message because you are subscribed to a topic in the Google Groups "civetweb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/civetweb/8Svmi34xnio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to civetweb+u...@googlegroups.com.
To post to this group, send email to cive...@googlegroups.com.
Visit this group at http://groups.google.com/group/civetweb.
To view this discussion on the web visit https://groups.google.com/d/msgid/civetweb/4aceb303-8228-4a84-8120-539262ede699%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

bel

unread,
Aug 15, 2014, 2:35:51 PM8/15/14
to cive...@googlegroups.com, bel...@gmail.com

Yes and yes, please ;-)

If it is included, it will be maintained with the code. A unit test would be helpful for this - but I can create the test on my own, just as you like.
I plan some adjustments of the C interface (civetweb.h), to skip some obsolete functions and get a consistent style there. Maybe we can discuss the C interface in advance.

Reply all
Reply to author
Forward
0 new messages