Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2018
    Messaggi
    2

    [C] problema invio HTTP Request google maps api

    Buongiorno a tutti, ho un problema con un programma, il cui scopo principale è spedire richieste GET a maps.googleapis.com e ricevere il file JSON da analizzare in seguito.
    Di seguito il codice del file main file main e di seguito l'header

    codice:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include "mysocketfn.h"
    
    #define HOST "maps.googleapis.com"
    #define PORT 443
    
    int main()
    {
        char request[]=
            "GET https://"
            HOST
            "/maps/api/distancematrix/json?origins=roma&destinations=milano&mode=driving&language=it-IT\r\n"
            "Host: maps.googleapis.com\r\n"
            "\r\n";
    
        send_message(HOST,PORT,request);
    
        return 0;
    }

    codice:
    /* mysocketfn.h */
    
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h> 
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    
    #define BUF_MAX 1000
    
    void send_message(char*addressname, int port, char*message_to_send)
    {
        struct hostent *hs;
        struct sockaddr_in serveraddr;
    
        fprintf(stdout,"Retrieving information about %s\n",addressname);
    //resolve hostname
        hs = gethostbyname(addressname);
        if (hs == NULL)   // do some error checking
        {
            fprintf(stdout,"Errore gethostbyname(%s)",addressname);
            herror("gethostbyname");
            exit(1);
        }
    
    // print information about this host:
        fprintf(stdout,"Official name is: %s\n", hs->h_name);
        fprintf(stdout,"IP address: %s\n", inet_ntoa(*(struct in_addr*)hs->h_addr));
    
        unsigned long ula = *((unsigned long *)hs->h_addr_list[0]);
    
        fprintf(stdout, "gethostbyname(%s) result: %lu\n",addressname, ula);
    
    //   Configure settings of the server address struct
    //    Set IP address
        serveraddr.sin_addr.s_addr=ula;
    //    Address family = Internet
        serveraddr.sin_family = AF_INET;
    //    Set port number, using htons function to use proper byte order
        serveraddr.sin_port = htons(port);
    
        memset(&serveraddr.sin_zero, 0, sizeof(serveraddr.sin_zero));
    
        fprintf(stdout,"*Address in the correct form: %s\n",inet_ntoa(serveraddr.sin_addr));
    
    //***********************************************************************************
    //* CREATE SOCKET, CONNECT, SEND MESSAGE, RECEIVE RESPONSE, CLOSE SOCKET CONNECTION *
        int sock;   // socket descriptor
        int chkerr;
        char buff[BUF_MAX]; 
    
        fprintf(stdout,"Creating socket...\n");
    
    //Create socket descriptor
        sock=socket(AF_INET,SOCK_STREAM,0);
        if (sock < 0)
            fprintf(stdout,"Error creating socket.\n");
        else fprintf(stdout,"Socket created\n");
    
        fprintf(stdout,"Connecting to %s on port %d...\n",inet_ntoa(serveraddr.sin_addr),port);
    
    //Connect to server
        chkerr=connect(sock,(struct sockaddr *)&serveraddr, sizeof(serveraddr));
        if (chkerr<0)
            fprintf(stdout,"Error connecting to the server\n");
        else fprintf(stdout,"Connected to server\n");
    
        fprintf(stdout,"Sending data...\n");
    
    //Send data
        fprintf(stdout,"Data:\n%s\n",message_to_send);
        chkerr=send(sock,message_to_send,sizeof(message_to_send),0);
        if (chkerr<0)
            fprintf(stdout,"Data sending error\n");
        else fprintf(stdout,"Data send\n");
    
        fprintf(stdout,"Server response:\n\n");
    
    //Receive response
        memset(buff,0,sizeof(buff));
        while((chkerr=recv(sock,buff,sizeof(buff),0)) != 0)
        {
            if (chkerr<0)
                fprintf(stdout,"Data reception error\n");
            else if (chkerr==0) fprintf(stdout,"End of data reception");
            else fprintf(stdout,"%s",buff);
        }
    
    //CLOSE socket descriptor
        close(sock);
        fprintf(stdout,"Connection closed\n");
    
    }

    l'output:
    Retrieving information about maps.googleapis.com
    Official name is: googleapis.l.google.com
    IP address: 216.58.205.138
    gethostbyname(maps.googleapis.com) result: 7453298398822480600
    *Address in the correct form: 216.58.205.138
    Creating socket...
    Socket created
    Connecting to 216.58.205.138 on port 443...
    Connected to server
    Sending data...
    Data:[/CODE]
    GET https://maps.googleapis.com/maps/api...language=it-IT
    Host: maps.googleapis.com


    Data send
    Server response:

    Connection closed

    nessuna risposta, ma se provo:
    codice:
    int main()
    {
    send_message("google.com",80,"GET /\r\n\r\n");
    
    return 0;
    }

    ecco l'output
    Retrieving information about google.com
    Official name is: google.com
    IP address: 216.58.205.174
    gethostbyname(google.com) result: 2932685528
    *Address in the correct form: 216.58.205.174
    Creating socket...
    Socket created
    Connecting to 216.58.205.174 on port 80...
    Connected to server
    Sending data...
    Data:
    GET /


    Data send
    Server response:

    HTTP/1.0 302 Found
    Cache-Control: private
    Content-Type: text/html; charset=UTF-8
    Referrer-Policy: no-referrer
    Location: http://www.google.it/?gfe_rd=cr&dcr=...-MNrPCXqTXseAM
    Content-Length: 266
    Date: Fri, 12 Jan 2018 14:06:48 GMT

    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>302 Moved</TITLE></HEAD><BODY>
    <H1>302 Moved</H1>
    The document has moved
    <A HREF="http://www.google.it/?gfe_rd=cr&amp;dcr=0&amp;ei=-MBYWp-MNrPCXqTXseAM">here</A>.
    </BODY></HTML>
    Connection closed

    quindi mi chiedo...perchè non ricevo niente inviando a googleapis? sto sbagliando a livello di c o la richiesta http?
    Grazie mille in anticipo...
    Ultima modifica di klavdio; 13-01-2018 a 14:33

  2. #2
    Utente di HTML.it L'avatar di linoma
    Registrato dal
    Mar 2010
    Messaggi
    1,346
    Intanto all'occhio viene subito fuori che la richiesta la fai ad un sito HTTPS, quindi dovresti usare una connessione SSL, cifrata. Ed ha un protocolo cifrato e diverso da quello che usi tu, quindi fatta eccezione per HELO il tutto è praticamente illegibile. Quindi il motivo per cui sulla porta 80 tutto funge.
    Per gli Spartani e Sparta usa spartan Il mio github

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2018
    Messaggi
    2
    Ok, me lo fa lo stesso anche con http..scusa ma non sono pratico di http... dunque, come devo procedere?

  4. #4
    Utente di HTML.it L'avatar di linoma
    Registrato dal
    Mar 2010
    Messaggi
    1,346
    Non fa la stessa cosa con http, da quello che posti nn mi sembra proprio, ti da una risposta ed anche sensata. Prova a cercare con google librerie ssl ce ne sono diverse, almeno da quello che ricordo.
    Per gli Spartani e Sparta usa spartan Il mio github

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2018
    Messaggi
    2
    Grazie mille! mi butto a studiarle un pò openssl e spero di riuscire a finire...

  6. #6
    Utente di HTML.it L'avatar di linoma
    Registrato dal
    Mar 2010
    Messaggi
    1,346
    Se sei costretto ad usare i socket credo che le alternative siano poche. Viceversa ci sn API già belle e pronte in ambiente Windows se non ricordo male. Cmq qualche temp fai feci un lavoretto qui che usava le librerie cyassl.
    Per gli Spartani e Sparta usa spartan Il mio github

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.