通过本地代理发送GET到谷歌。 C ++。 Linux操作系统。 TCP

时间:2014-09-22 00:22:30

标签: c++ linux sockets tcp proxy

我正在尝试创建一个网络抓取工具,我希望它能够通过本地代理连接到网站。

所以,我们要说我们要向谷歌发送GET消息并检索它的HTML代码,所有这一切都通过本地代理(我在我的大学工作,并且有用于连接外部网站(如google)的代理。

这是我的代码:

#include <iostream>
#include <cstring>      // Needed for memset
#include <sys/socket.h> // Needed for the socket functions
#include <netdb.h>      // Needed for the socket functions
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
addrinfo host_info;       // The struct that getaddrinfo() fills up with data.
addrinfo *host_info_list; 
int socketfd;
char* msg = NULL;
char* msg2 = NULL;
int status;
int len;

memset(&host_info, 0, sizeof host_info);

host_info.ai_family = AF_INET;//AF_UNSPEC;
host_info.ai_socktype = SOCK_STREAM;

//PROXY IP = proxy.fing.edu.uy ; PORT = 3128 ; //HTTP1.0 proxy

status = getaddrinfo("proxy.fing.edu.uy", "3128", &host_info, &host_info_list);

socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype, 
host_info_list->ai_protocol);

if (socketfd == -1)  std::cout << "ERROR: socket error " << std::endl ; 

std::cout << "Connect()ing..."  << std::endl;

status = connect(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);
if (status == -1)  std::cout << "ERROR: connect error" << std::endl ;

msg = new char[200];
strcpy(msg,"CONNECT www.google.com HTTP/1.0\r\n");
strcat(msg,"\r\n");


ssize_t bytes_sent;
len = strlen(msg);
bytes_sent = send(socketfd, msg, len, 0);

ssize_t bytes_recieved=0;
std::cout << "Waiting to recieve data..."  << std::endl;

char* incoming_data_buffer = new char[200];
bytes_recieved = recv(socketfd, incoming_data_buffer,200, 0);

if (bytes_recieved == 0) std::cout << "host shut down." << std::endl ;
if (bytes_recieved == -1)std::cout << "ERROR: receive error!" << std::endl ;
std::cout << bytes_recieved << " bytes recieved" << std::endl ;
std::cout << incoming_data_buffer << std::endl;


msg2 = new char[300];
strcpy(msg2,"GET http://www.google.com/ HTTP/1.0\r\n\r\n");


std::cout << "Message sent to google: " << msg2 << std::endl;

len = strlen(msg2);
bytes_sent = send(socketfd, msg2, len, 0);

cout << "bytes_sent: " << bytes_sent << endl;

bytes_recieved=0;
std::cout << "Waiting to recieve data ..."  << std::endl;

char* incoming_data_buffer2 = new char[1000];
bytes_recieved = recv(socketfd, incoming_data_buffer2,1000, 0);


if (bytes_recieved == 0) std::cout << "host shut down." << std::endl ;
if (bytes_recieved == -1)std::cout << "ERROR: recieve error!" << std::endl ;
std::cout << bytes_recieved << " bytes recieved" << std::endl ;
std::cout << incoming_data_buffer2 << std::endl;
return 0;
}

我遇到的问题如下...... 首先,incoming_data_buffer(来自&#34; CONNECT&#34;的缓冲区)返回:&#34;已建立HTTP1.0 200连接&#34;,这是好的,直到现在都没有问题。 接下来我发送&#34; GET&#34;消息发送到代理,以便它按预期将消息转发到谷歌(现在已建立连接),并且它在&#34; recv()&#34;中保持空闲状态。大约1分钟左右,然后它返回0(这意味着我想连接关闭),缓冲区为空... 我的问题是,我不知道为什么recv()返回0 ...任何想法?据说这意味着连接已关闭,但为什么呢?为了让代理保持连接,我还需要做些什么? (假设&#34;连接关闭&#34;是问题)。

提前致谢!

1 个答案:

答案 0 :(得分:0)

CONNECT方法是HTTP隧道功能。支持它的代理可能会限制其连接到HTTPS网站的使用(来源:Wikipedia -- HTTP tunnel)。您正尝试通过代理可能阻止的CONNECT与标准HTTP服务器建立连接。

与代理建立连接后,只需发送您的请求,而不是建立隧道。这可行,因为您使用absoluteURI来指定GET目标。