开发者

Get data from the web server and sent them to the client c++

I am trying to get some data from the webserver as a proxy,and then sent the data to the client.

Webserver -> Me -> Client

This is my main code:

int main()
{
    Initialize();
    ServerSocket s;
    s.Bind();
    s.Listen();
    while(true)
    {
        TCPSocket* sock = s.Accept();

        ClientSocket client;
        string addr = handle.getAddress();
        short prt = handle.getPort();

        client.Connect(addr, prt);
        char data[5000];
        client.Send("GET /index.html HTTP/1.0\r\n\r\n", 28) ;
        while(true)
        {
            int numbytes=client.Recv(data, 5000);
            if(numbytes == 0)
            {
                break;
            }
  开发者_JAVA百科          cout <<"Received from web server: " << numbytes << endl;
            int numbytes2 = sock->Send(data, numbytes);
            cout << "Sent to client: " << numbytes2 << endl;
        }
        client.Close();
    }
    s.Close();
    return 0;
}

But when I browse the net,lets say google.com, I get "302 Moved The document has moved here." In every site I get different behavior,but the site won't load up. Am i doing something wrong when I send and receive bytes ? Thanks in advanced.


Your code is working just fine. Its just that modern day webservers won't allow you to connect without properly authenticating yourself, using user-agen, headers etc. Try the following commands on your shell.

telnet yourserver.com 80
<some messages from server>

GET /index.html HTTP/1.0

If the response is same as what you get using C++ program, then basically all you need to do is send more info to properly authenticate yourself.

  user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
  headers = {'User-Agent': user_agent}

And send this data along with your GET request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜