乐闻世界logo
搜索文章和话题

How to make an HTTP get request in C without libcurl?

1个答案

1

Sending an HTTP GET request in C without libraries such as libcurl requires low-level socket programming. This process involves creating and configuring sockets, establishing a connection to the target server, and manually sending crafted HTTP requests. Below is a basic step-by-step guide and example code using socket functions from the standard C library to accomplish this task:

Steps

  1. Initialize the socket library (required only on Windows systems):

    • Windows systems require initializing WSA (Windows Sockets API) using the WSAStartup function.
  2. Create a socket:

    • Use the socket function to create a socket. For HTTP, TCP protocol is typically used, so the socket type is SOCK_STREAM and the protocol is IPPROTO_TCP.
  3. Connect to the server:

    • Use gethostbyname to resolve the server's IP address.
    • Use the connect function to establish a connection to the server's specific port (HTTP typically uses port 80).
  4. Send the HTTP GET request:

    • Manually construct a simple HTTP GET request string.
    • Use the send function to transmit the request to the server.
  5. Receive the response:

    • Use the recv function to receive the response from the server.
    • Process or output the response data.
  6. Close the socket:

    • Use closesocket on Windows or close on UNIX/Linux to close the socket.
  7. Cleanup the socket library (required only on Windows systems):

    • Use the WSACleanup function.

Example Code

c
#include <stdio.h> #include <string.h> #include <stdlib.h> #ifdef _WIN32 #include <winsock2.h> #else #include <sys/socket.h> #include <arpa/inet.h> #include <netdb.h> #include <unistd.h> #endif int main() { #ifdef _WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); return 1; } #endif int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { perror("Socket creation failed"); return 1; } struct hostent *server = gethostbyname("example.com"); if (server == NULL) { fprintf(stderr, "Host resolution failed.\n"); return 1; } struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); serv_addr.sin_port = htons(80); if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("Connection failed"); return 1; } char *message = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"; if (send(sock, message, strlen(message), 0) < 0) { perror("Send failed"); return 1; } char response[4096]; if (recv(sock, response, sizeof(response), 0) < 0) { perror("Receive failed"); return 1; } printf("Server response: %s\n", response); close(sock); #ifdef _WIN32 WSACleanup(); #endif return 0; }

In this example, we manually construct an HTTP GET request and send it via sockets. Note that this approach requires a thorough understanding of the HTTP protocol and TCP/IP, particularly when dealing with more complex HTTP requests and responses. In commercial and production environments, for security and usability, it is generally recommended to use established libraries such as libcurl.

2024年7月23日 11:16 回复

你的答案