Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

HTTP Response Codes

comrade009comrade009 Member Posts: 8
I'm a beginner C++ programmer whose completed a course that uses the
lawrenceville press "Intro to C++" book, if any of you are familiar
with it. My question is how can i:

1) Call up a web page
2) Get the http response code (like 404 or 200 for example)
3) Store the code as a variable

The libraries I'm most familiar with are iostream.h and fstream.h, but
if you have an idea with a different library thats fine.

Comments

  • abonghit1abonghit1 Member Posts: 71

    // i hope this isnt for school and thats why nobody replied.....
    // here is a quick way to get a response from an http server (GET /index.htm).
    // it doesnt work on at least one server (see an earlier post from me ;)
    // because the http headers are not http 1.1 (rfc2068?) compliant.
    // the code is sloppy and barely scratches the surface of network
    // programming but it will communicate with most http servers.
    // btw folks plz no flames, ive noticed you guys/girls are really skilled
    // and know the small details that, for now, elude me.
    // note that you should use wsagetlasterror() for better error codes,
    // actually i found a good function to translate the error codes into
    // strings, very helpful, from the wormhole project (url??) called
    // sockerrtostring(). very useful, but it aint mine so it aint here.
    // you need to use ip addys only, its a different function to resolve the name.
    // note a useful resource for windows network programming is winsock.chm from
    // the msdn library.
    // this compiled with bcc32 command line on windows 95 osr2.
    // btw i am (ass)uming youre using windoze!
    //

    #include
    #include

    using namespace std;

    int main()
    {
    // the ip of the remote server
    char *server = "127.0.0.1";

    WSADATA wsa; // structure that winsock uses
    SOCKET sock; // socket
    sockaddr_in sa_in; // structure for server info
    WORD version = MAKEWORD(2, 2); // winsock version 2

    // stuff
    int iret, ircv;

    // buffers for http headers (send) and data sent back (recieve)
    char send_hdr[127], recv_bfr[255];

    // minimal http headers for http v1.0
    // if you use version 1.1 you have to send a whole bunch of headers
    // like user-agent, accept:, etc...
    // note that there are 2 '
    's, just like telnet
    // where you need to hit enter twice
    char http_hdr[] = "HTTP/1.0

    ";

    // try to start winsock
    if (WSAStartup(version, &wsa) == 0) {

    // set up a socket
    sock = socket(AF_INET, SOCK_STREAM, 0);
    // be a good boy and check for errors
    if (iret == SOCKET_ERROR) {cout << "sockerr socket()"; return 1;}

    // set up the destination address stuff

    // address family
    sa_in.sin_family = AF_INET;

    // port - htons() turns "80" into network byte order
    // (x86 uses host byte order, cant-remember-endian)
    sa_in.sin_port = htons(80); // port 80

    // convert the decimal ipv4 addr into the right format
    sa_in.sin_addr.s_addr = inet_addr(server);

    // try to connect to the remote machine (on port 80)
    iret = connect(sock,(sockaddr *)&sa_in, sizeof(sa_in));
    // error check again
    if (iret == SOCKET_ERROR) {cout << "sockerr connect()"; return 1;}

    // set up the simple request header
    strcpy(send_hdr, "GET /index.htm ");
    strcat(send_hdr, http_hdr);

    // send it to the remote machine
    iret = send(sock, send_hdr, strlen(send_hdr), 0);
    if (iret == SOCKET_ERROR) {cout << "sockerr send()"; return 1;}

    // set up a loop for receiving data
    // there are probably 100 better ways to do this but this is
    // quick (and dirty)
    // when we recieve 0 bytes the loop will end
    ircv = 1;
    while(ircv != 0) {
    // clear buffer
    strset(recv_bfr, ' ');
    //recieve data
    ircv = recv(sock, recv_bfr, sizeof(recv_bfr), 0);
    if (iret == SOCKET_ERROR) {cout << "sockerr recv()"; return 1;}
    // send data to stdout
    cout << recv_bfr;
    }
    }
    else { cout << "error wsastartup()"; WSACleanup(); return 1;}

    // clean up the winsock stuff
    WSACleanup();

    // goodbye..
    return 0;
    }

    // the first line in the result will have the data you want:
    // HTTP/1.x 200 OK
    // HTTP/1.x 404 NOT FOUND
    // etcetera
    // hope this helps


    : I'm a beginner C++ programmer whose completed a course that uses the
    : lawrenceville press "Intro to C++" book, if any of you are familiar
    : with it. My question is how can i:
    :
    : 1) Call up a web page
    : 2) Get the http response code (like 404 or 200 for example)
    : 3) Store the code as a variable
    :
    : The libraries I'm most familiar with are iostream.h and fstream.h, but
    : if you have an idea with a different library thats fine.
    :
    :

  • Justin BibJustin Bib USAMember Posts: 0

    __ < http://forcoder.org /> free video tutorials and ebooks about [ JavaScript, Visual Basic, C, C#, C++, Perl, Objective-C, Swift, Ruby, Go, Delphi, Scratch, Visual Basic .NET, R, Java, Python, PHP, MATLAB, Assembly, PL/SQL Kotlin, FoxPro, Bash, Scheme, Hack, Dart, SAS, F#, Crystal, Rust, Lua, ABAP, Apex, VBScript, LabVIEW, Fortran, Transact-SQL, D, Scala, ML, Julia, Logo, Clojure, Erlang, Lisp, COBOL, Prolog, Awk, Alice, Ada ] ________

Sign In or Register to comment.