Ci ho provato,codice:/* ** http.c -- a stream socket HTTP client demo */ /*Uncomment for your system*/ /*#define LINUX 1*/ /*#define WINDOWS 1*/ #ifdef LINUX #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #endif #ifdef WINDOWS #include <winsock.h> #endif #define PORT 80 // the port client will be connecting to #define MAXDATASIZE 1024 // max number of bytes we can get at once #define HTTP_GET "GET %s HTTP/1.1\nHost: %s\n\n" int main(int argc, char *argv[]) { #ifdef WINDOWS WSADATA wsaData; #endif int sockfd, numbytes, length; char buf[MAXDATASIZE]; char request[196]; char *response = NULL; struct hostent *he; struct sockaddr_in their_addr; // connector's address information if (argc != 2) { fprintf(stderr, "Usage:\t%s <hostname> <path>\n", argv[0]); exit(1); } #ifdef WINDOWS if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); } #endif if ((he=gethostbyname(argv[1])) == NULL) { // get the host info herror("gethostbyname"); exit(1); } if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero); if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof their_addr) == -1) { perror("connect"); exit(1); } sprintf(request, HTTP_GET, argv[2], argv[1]); if ((numbytes=send(sockfd, request, strlen(request), 0)) == -1) { perror("send"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; sscanf(buf, "Content-Length: %d", &length); response = (char*)malloc(sizeof(char)*length); for(; numbytes < length; numbytes += recv(sockfd, buf, MAXDATASIZE-1, 0)){ strcat(response, buf); buf[numbytes] = '\0'; } printf("%s\n",response); free(response); #ifdef LINUX close(sockfd); #else WSACleanup(); #endif return 0; }
non so se funzioni,
so che è pieno di errori,
ma credo che sia un punto di partenza.![]()
![]()