I want to do something like the wget function in Linux. The problem is that I get a normal html response only for www.google.com, such as what is returned for www.mail.ru

HTTP/1.1 301 Moved Permanently Server: nginx/1.6.2 Date: Tue, 04 Dec 2018 05:56:28 GMT Content-Type: text/html Location: http://mail.ru/ X-Frame-Options: SAMEORIGIN Content-Length: 184 Connection: close Age: 0 <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.6.2</center> </body> </html> 

Below is the request code, I think the problem is in this post:

 <br>GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: fetch.c\r\n\r\n 

I send it in the read() function.

This is how I create a socket:

 int socket_desc = socket(AF_INET, SOCK_STREAM, 0); if (socket_desc == -1) { puts("could not create socket"); } char* ip = new char; getHost_byName(hostname, ip); int success = read(ip, socket_desc, hostname); if (success != 0) { puts("Error"); } close(socket_desc); 

In this function, I send a message and get the answer:

 int read(char* address, int socket_desc, char* hostname) { struct sockaddr_in server; server.sin_addr.s_addr = inet_addr(address); server.sin_family = AF_INET; server.sin_port = htons( 80 ); //Connect to remote server if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0) { puts("connect error"); return 1; } puts("Connected\n"); puts(hostname); char* message; const char * format = "GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: fetch.c\r\n\r\n"; int status = asprintf(& message, format, hostname); if (status == -1) { printf("asprintf doesn't work"); return 1; } //puts(message); if (send(socket_desc, message, strlen(message), 0) < 0) { puts("send failed"); return 1; } puts("data send"); //-recv - calls are used to receive messages from a socket int size_recv , total_size= 0, BLOCK_SIZE = 512; char chunk[BLOCK_SIZE]; ofstream fout(getFileName(hostname)); //loop while(1) { memset(chunk ,0 , BLOCK_SIZE); //clear the variable if((size_recv = recv(socket_desc , chunk , BLOCK_SIZE, 0) ) < 0) { break; } else { fout << chunk; printf("%s" , chunk); } } fout.close(); return 0; } 
  • one
    Your code has nothing to do with it - this is redirection, the normal answer is 301, which you should work out in the program and request the page at the new address specified in the answer (by the way, with tracking where you already were - so as not to get into a closed loop if someone is wrong server will configure :)). So to say, read the documentation for the protocol http ... - Harry
  • Clearly, thanks for the advice, I'll go look for how it is done and what they eat it with. - Leto
  • This is the question: gethostbyname, this function, if you give it http: // + mail.ru/, what is in the response in the location field returns the answer Unknown host. Why can this be? n - Leto
  • Because you need to send her only the name, not the URL - mail.ru - Harry
  • Now I get 500 Internal server error - Leto

0