Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Bad request HTTP, chociaż request jest taki sam jak w mozilli

Ostatnio zmodyfikowano 2017-12-10 17:29
Autor Wiadomość
Szustarol
Temat założony przez niniejszego użytkownika
Bad request HTTP, chociaż request jest taki sam jak w mozilli
» 2017-12-07 16:27:24
Witam!
próbuję pobrać zawartość strony example.com, takim programem:

C/C++
#include <iostream>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>

using namespace std;

std::string writeToSocket( int fd, std::string contents, bool retry = true ) { // returns string which has not been written, or retries to send it
    for( unsigned i = 0; i < contents.size(); ) {
        int written = write( fd, & contents[ i ], sizeof( contents ) - i );
        if( written == 0 ) {
            break;
        }
        if( written == - 1 ) {
            bool ctn = false;
            switch( errno ) {
            case EINTR:
                ctn = true;
                break;
            default:
                std::cout << "Error when writing to the socket!";
                break;
            }
            if( ctn == false )
                 break;
            else
                 continue;
           
        }
        i += written;
        if( i != contents.size() && retry == false ) {
            return contents.substr( i, contents.size() );
        }
    }
    return "";
}

std::string readFromSocket( int fd ) {
    std::string container;
    const int chunksize = 32;
    container.resize( chunksize, 0x0 );
    int position = 0;
    while( true ) {
        int rd = read( fd, & container[ position ], chunksize );
        if( rd > 0 ) {
            container.resize( container.size() + chunksize );
            position += rd;
            continue;
        }
        if( rd == 0 ) {
            break;
        }
        if( rd < 0 ) {
            if( errno == EINTR ) {
                continue;
            }
            else {
                break;
            }
        }
    }
    return container.substr( 0, position );
}

int main( /*int argc, char *argv[]*/ )
{
    sockaddr_in ServerAddress = { };
    inet_pton( AF_INET, "93.184.216.34", & ServerAddress.sin_addr ); //example.com
    ServerAddress.sin_port = htons( 80 );
    ServerAddress.sin_family = AF_INET;
    int connectionFeed;
    int errorControl;
   
    connectionFeed = socket( AF_INET, SOCK_STREAM, 0 );
   
    if(( errorControl = connect( connectionFeed,( sockaddr * ) & ServerAddress, sizeof( ServerAddress ) ) ) < 0 ) {
        std::cout << "Problem with binding socket " << errno << std::endl;
        exit( errno );
    }
   
    writeToSocket( connectionFeed, "GET / HTTP/1.1 \r\n\r\n" );
    std::cout << readFromSocket( connectionFeed ) << std::endl;
   
   
    close( connectionFeed );
    return 0;
}
Niestety, zwraca mi:


HTTP/1.1 400 Bad Request
Content-Type: text/html
Content-Length: 349
Connection: close
Date: Thu, 07 Dec 2017 15:27:03 GMT
Server: ECSF (lga/1318)

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
                <title>400 - Bad Request</title>
        </head>
        <body>
                <h1>400 - Bad Request</h1>
        </body>
</html>
Czy ktoś mógłby mi przetłumaczyć gdzie jest błąd w moim requeście?
P-167706
killjoy
» 2017-12-07 16:48:07
Brak pola "Host:" w nagłówku http.
P-167707
Breakermind
» 2017-12-10 17:14:59
Może użyj curl-a i nie kombinuj https://curl.haxx.se/libcurl/c/example.html
P-167773
Rashmistrz
» 2017-12-10 17:29:40
C/C++
const char * http_request = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
writeToSocket( connectionFeed, http_request );

Powinno zadziałać.

Hypertext Transfer Protocol -- HTTP/1.1
HTTP requests
P-167776
« 1 »
  Strona 1 z 1