从 Web 服务器获取数据并将其发送到客户端 c++
我正在尝试从网络服务器作为代理获取一些数据,然后将数据发送到客户端。
网络服务器 ->我->客户端
这是我的主要代码:
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;
}
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;
}
但是当我浏览网络时,比如说google.com,我得到“302 Moved 该文档已移至此处”。在每个网站中我都会得到不同的行为,但该网站不会加载。当我发送和接收字节时我做错了什么吗?提前致谢。
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;
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码工作得很好。只是现代网络服务器不允许您在没有使用用户代理、标头等正确验证自己身份的情况下进行连接。请在 shell 上尝试以下命令。
如果响应与您使用 C++ 程序得到的响应相同,那么基本上您需要做的就是发送更多信息来正确验证自己的身份。
并将此数据与您的 GET 请求一起发送。
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.
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.
And send this data along with your GET request.