Hardware Upgrade Forum

Hardware Upgrade Forum (https://www.hwupgrade.it/forum/index.php)
-   Programmazione (https://www.hwupgrade.it/forum/forumdisplay.php?f=38)
-   -   [C] problema invio HTTP Request google maps api (https://www.hwupgrade.it/forum/showthread.php?t=2840912)


marilynm69 12-01-2018 20:10

[C] problema invio HTTP Request google maps api
 
Ciao 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.
Sto scrivendo su linux,questo è il mio codice, 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");

}

questo è ciò che ottengo:
Codice:

...
Connecting to 216.58.205.138 on port 443...
Connected to server
Sending data...
Data:
GET https://maps.googleapis.com/maps/api...language=it-IT
Host: maps.googleapis.com


Data send
Server response:

Connection closed

se come prova faccio:
Codice:

int main()
{
send_message("google.com",80,"GET /\r\n\r\n");

return 0;
}

il risultato ...
Codice:

...
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

dunque il messaggio viene inviato senza problemi...
quindi mi chiedo...perchè non ricevo niente inviando a googleapis? sto sbagliando a livello di c o la richiesta http?

Scusate se non mi sono spiegato bene e grazie mille in anticipo

Pbdz 13-01-2018 13:57

il problema potrebbe risiedere nell'header della richiesta http.

Prova a modificare la variabile request[] così:

Codice:

char request[]=
        "GET /maps/api/distancematrix/json?origins=roma&destinations=milano&mode=driving&language=it-IT HTTP/1.1\r\n"
        "Host: maps.googleapis.com\r\n"
        "\r\n";


marilynm69 14-01-2018 14:35

provato lo stesso, mi da:
Quote:

Data send
Server response:

Connection closed


Tutti gli orari sono GMT +1. Ora sono le: 13:19.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Hardware Upgrade S.r.l.