通过本地代理发送GET到谷歌,C++,Linux,TCP

sshcrbum  于 2023-02-17  发布在  Linux
关注(0)|答案(1)|浏览(156)

我正在尝试创建一个网络爬虫,我希望它能够通过本地代理连接到网站。
所以,让我们说,我们想发送一个GET消息到谷歌和检索它的HTML代码,所有这一切都通过一个本地代理(我在我的大学工作,有一个代理连接到外部网站,如谷歌).
这是我的代码:

#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.feng.edu.uy ; PORT = 3318; //HTTP1.0 proxy

status = getaddrinfo("proxy.feng.edu.uy", "3318", &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(来自“CONNECT”的缓冲区)返回:“HTTP1.0 200连接已建立”,这很好,直到现在都没有问题,接下来我向代理发送“GET”消息,以便它按预期将消息转发给Google(现在连接已建立),并在“Recv()”中保持空闲1分钟左右,然后它返回0(我猜这意味着连接关闭了),缓冲区为空...我的问题是我不知道为什么recv()返回0...有什么想法吗?这意味着连接被关闭了,但是为什么呢?我还需要做什么才能让代理维护连接呢?(假设“连接关闭”是问题所在)。
先谢了!

atmip9wb

atmip9wb1#

CONNECT方法是一种HTTP隧道功能。支持该功能的代理可能会将其使用限制为连接到HTTPS网站(源代码:Wikipedia -- HTTP tunnel)。您正尝试通过CONNECT与标准HTTP服务器建立连接,代理可能会阻止该连接。
当你与代理建立连接后,不需要建立隧道,只需发送你的请求就可以了,因为你使用absoluteURI来指定你的GET目标。

相关问题