Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di Neocron
    Registrato dal
    Jul 2002
    Messaggi
    524

    [C] Sockets: prelevare contenuto pagina web

    Ragazzi avete qualche codice? O qualche buon consiglio?

    Vorrei inviare delle variabili post se possibile, com posso fare?
    Per quelle get potrei metterle nell'indirizzo...

    Ma come posso, dove aver inviato le variabili, prendere il contenuto della pagina in una stringa?

    Grazie.

  2. #2
    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;
    }
    Ci ho provato,
    non so se funzioni,
    so che è pieno di errori,
    ma credo che sia un punto di partenza.


  3. #3
    Utente di HTML.it L'avatar di Neocron
    Registrato dal
    Jul 2002
    Messaggi
    524
    Grazie, lo apprezzo molto.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.