I've googled for a while and found few things (I hate using external libs such as cURL) and I don't really have experience with HTTP, so the question is: how can I interact with websites using sockets? so something like this:

  1. I connect to the website
  2. I store value of variable, for example "var1" (on website) to my variable in the program "iVarValue"
  3. I type in iVarValue to the edit box with id "editbox1"
  4. I press button with id "button1"

so far I have only connection phase

#include <WinSock2.h>
#include <Windows.h>
#include <iostream>

using namespace std;

#pragma comment (lib, "ws2_32.lib")

int main()
    WSADATA WsaData;
    if(WSAStartup(MAKEWORD(2,2), &WsaData) != 0)
        cout << "startup failed\n";
        return 1;
    struct hostent *host;
    host = gethostbyname("www.google.com");
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port = htons(80);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    cout << "Connecting\n";
    if(connect(sock, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0)
        cout << "Can't connect\n";
        return 1;
    cout << "Connected!\n";


    cout << endl;
    return 0;

so how do I get the specific variable (not whole source code) and interact with windows?


Well, basically you are writing your own web browser - good luck! Not impossible, but not trivial (to say the least). You will have to be able to parse the returned HTML text and render it in some sort of reasonable way. FWIW, my company does this, and we utilize Mozilla (Firefox) code instead of rolling our own - and we are a multi-billion $ company... And we DO contribute back into the open-source community changes we make. :-)

well I kinda wnat to do travian bot, not my own browser :P anyway

I've been learning HTTP and coding this for 13 hours straight yesterday, yet I still have problem, there are 2 cases:

  1. I can go to the login site and login, but I can't get source code of login website (for login ID, which looks like it's unnecessary, but I still want to have it done properly)

  2. I can go to the login site, get source code with login id but I can't login

here's my source code:


here's the account information u can test it on:

server - ts1.travian.com
username - cppacc123
password - cpppw123

here's GET and POST request with their replies taken from webbrowser when manually logging in


program results:

  1. dont get source code, correct username and password:
  • reply.txt contains only header - works as intended
  • login.txt contains both header and source code - works as intended - however the header is somewhere in 1/4 of the source code, which is not good
  1. dont get source code, incorrect username or password
  • reply.txt contains only header - works as intended
  • login.txt contains both header and source code (of login website since we didnt login) - works as intended - however the header is almost at the very bottom, which is not good
  1. get source code, correct username and password
  • reply.txt contains header and source code - works as intended
  • login.txt contains bunch of "ĚĚĚĚĚĚĚĚĚĚĚĚĚĚĚĚĚĚĚ" - doesn't work at all
  1. get source code, incorrect username or password
  • same as 3.

also when getting any source code it takes a long time, even when the source code has 12kb (login) or ~77kb (actual game)

also when I send "Accept-Encoding: gzip, deflate" - even reply.txt won't be right then, so Im just skipping, Im skipping agent since it's not necessary (at least that guy I was talking with yesterday said it)

any suggestions?

(I hate using external libs such as cURL

Why try to reinvent the wheel. Using cUrl is simple?

simply I hate doing that, when I can do something with plain win32api without any external libs Im willing to learn that, with external libs I'd have to download libs and dlls over and over and include them in the application, simply I like to do everything in plain win32 api, that's why Im not using for example MFC or any other libraries, when there's something I will need in other applications I just create my own scripts/libraries, I also know how to use them and stuff... anyway, can you help me solve that issue?

basically I need :

  • reply.txt that contains both header and source code - works as intended (from 3.)
  • login.txt that contains both header and source code - works as intended (from 1.) BUT the header to be at top

No I don't like re-inventing the wheel.

there's nothing about reinventing wheel, there's a error withing http requests.... u're just stupid

commented: good luck +14
commented: Thats not being very polite -2

Good luck, I'm sure someone can help you.

Unlike iamthwee, I also enjoy "plain ole sockets", it can be useful to understand in a lot of cases.

I looked through your source and saw no "overt" errors at a glance.
I did wonder, are you sure that static cookie does what you think it does?

If you accept encoded data, that means you're telling the remote apache host it's OK to zip the packet before sending it to you, and you'll know how to unzip and interpret it to save bandwidth.

P.S. It's really helpful to people who want to help you if you can narrow down your problem to under 100 lines of code.

actually I fixed the problem but now I have another one:

when I want to use the same socket connected to the host to send() and recv() for the second time, recv() will return 0 without anything in buffer

basically Im doing:

connect to the website
send packets
receive packets
send packets again (I think this one is working since it's not giving me SOCKET_ERROR)
receive packets again (this one is returning 0 so as "connection closed")
source code: http://pastebin.com/sm5k5GAe

as you can see, I have sock2, which when I use for second send/recv it's working fine, however I will do more communication, and having sockets for all of that and connecting them would be kinda stupid I guess

I changed few things, now Im using only ONE socket and I'm keeping connection alive (sending Connection: keep-alive) in the first header, sending nothing in the second because by default the connection should be still alive, yet I'm receving 0 from second recv but getting the first reply without any problem

edit: look at the functions GetReply() and GetLoginReply()