hi everyone,:cool:

I like to work on accessing internet(that means get a web site content) with my c code. so plz tell me a possible way to send reqest to a perticular web site and get the information....:rolleyes:

Dani AI

Generated

Two practical routes exist for accessing a web page from C: use a mature HTTP client library for real work, or implement a minimal client on top of sockets to learn how the protocol works. ’s suggestion to read the HTTP spec is sound, but note that the old RFC 2616 was obsoleted in 2014 — consult the modern HTTP/1.1 specification documents (for message framing and semantics see RFC 7230 and RFC 7231). (rfc-editor.org)

For the fastest path to a working program, use libcurl: it handles HTTP/1.1/2, redirects, TLS, cookies, proxies, and offers a simple C API. A minimal example (prints the response body to stdout) shows how compact a client can be:

#include <stdio.h>
#include <curl/curl.h>

static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *userdata) {
    return fwrite(ptr, 1, size * nmemb, stdout);
}

int main(void) {
    CURL *c; CURLcode res;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    c = curl_easy_init();
    if(!c) return 1;
    curl_easy_setopt(c, CURLOPT_URL, "http://example.com/");
    curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_cb);
    res = curl_easy_perform(c);
    if(res != CURLE_OK) fprintf(stderr, "error: %s\n", curl_easy_strerror(res));
    curl_easy_cleanup(c);
    curl_global_cleanup();
    return (res == CURLE_OK) ? 0 : 1;
}

Compile with a command like gcc -o get get.c -lcurl. See the libcurl API docs for more options and examples. (curl.se)

If the goal is learning, implement a simple TCP client with the sockets API to send a plain HTTP/1.0/1.1 GET request, then add parsing for headers, Content-Length and chunked transfer. Beej’s Guide to Network Programming is an excellent step-by-step tutorial for sockets. Remember HTTPS adds TLS: either let libcurl handle it or layer TLS with a library such as OpenSSL. (beej.us)

’s point about searching is practical; for quick results start with the libcurl example, and for deeper understanding follow the sockets + RFC route ( will get both a working tool and the protocol knowledge).

Recommended Answers

All 3 Replies

If you want to build from scratch, read the HTTP Specification and implement it in C.
If you want a C API library, check curl.

thanks for reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.