Download html source

Dec 26, 2012 at 6:59pm
Hi!

I made a software wich print html code of a webpage in CLI. How I can download this source to my PC from my software?


Thanks.
Dec 26, 2012 at 9:02pm
You want to know how to write something to a file or what's the question?
http://www.cplusplus.com/doc/tutorial/files/
Last edited on Dec 26, 2012 at 9:03pm
Dec 27, 2012 at 10:37am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>

int main()
{
WSADATA wsaData;
int iResult;

iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
         printf("WSAStartup failed: %d\n", iResult);
return 1;
}
   struct sockaddr_in info;
   ZeroMemory(&info, sizeof(info));
info.sin_family = AF_INET;
info.sin_port = htons(80);
struct in_addr addr;
struct hostent *remoteHost;
remoteHost = gethostbyname("google.com");
printf("Official name: %s\n", remoteHost->h_name);
if (remoteHost->h_addrtype == AF_INET) {
          int i = 0;
while (remoteHost->h_addr_list[i] != 0) {
         addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
          info.sin_addr.s_addr = addr.s_addr;
printf("IP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
SOCKET s = INVALID_SOCKET;

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
iResult = connect(s, (struct sockaddr*)&info, sizeof(info));

if (iResult == SOCKET_ERROR)
{
       printf("connect failed: %d\n", WSAGetLastError());
}
char mesaj1[] = "GET / HTTP/1.0\r\nHost: www.google.com\r\nUser-Agent: Browser rudimentar\r\n\r\n";

iResult = send(s, mesaj1, sizeof(mesaj1), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
}
char receiveBuffer[128];
ZeroMemory(receiveBuffer, sizeof(receiveBuffer));
do
{
iResult = recv(s, receiveBuffer, 64, 0);
printf(receiveBuffer);

ZeroMemory(receiveBuffer, sizeof(receiveBuffer));
}
while (iResult > 0);


FILE *file; 
file = fopen("code.txt","a+"); 
fprintf(file,"%s",receiveBuffer); 
fclose(file);
getchar(); 
WSACleanup();
return 0;
}


The program show html page of google in CLI , create a file "code.txt", but this file is empty. Why?
Dec 27, 2012 at 7:11pm
You zero out the data you received in line 53 and you're surprised that your file is empty?
Besides, line 51 and 60 are wrong. Neither method works correctly if there are null bytes in your data and line 51 will also fail miserably if the data contains any formatting sequences (%d, %s, etc.)
Topic archived. No new replies allowed.