fire64
10.04.2009, 12:53
Каким образом отделить файл от заголовка http ?
Content-Type: image/gif
я знаю что сам контент файла начинается после Content-Type: image/gif
но как их разъединить, и еще, как сохранить в файл в буфер содержащий нули ?
#include<stdio.h>
#include<string.h>
#include<winsock2.h>
#include<windows.h>
#define HTTP_PORT 80
#define HTTP_IP "10.207.112.134"
//
// The Scan function
//
int main()
{
// This function does the actual scanning
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET hSocket;
// Initialize the socket
hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (hSocket == INVALID_SOCKET)
{
// Return FALSE in case of an error
return FALSE;
}
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = inet_addr ( HTTP_IP );;
sin.sin_family = AF_INET;
sin.sin_port = htons(HTTP_PORT);
fd_set fd_read, fd_write, fd_error;
timeval timeout;
timeout.tv_sec = 3; // TODO: Hardcoded, should be configurable
timeout.tv_usec = 0;
u_long nNonBlocking = 1;
// Set socket to non-blocking mode
ioctlsocket(hSocket, FIONBIO, &nNonBlocking);
BOOL bConnected = FALSE;
// Estabilish a TCP connection
connect(hSocket, (sockaddr*)&sin, sizeof(sin));
fd_write.fd_array[0] = hSocket; fd_write.fd_count = 1;
fd_error.fd_array[0] = hSocket; fd_error.fd_count = 1;
if (select(0, 0, &fd_write, &fd_error, &timeout) > 0)
{
if (fd_write.fd_count > 0)
{
// Connection successful
bConnected = TRUE;
}
}
char zapros[2048];
strcpy(zapros,"GET /");
strcat(zapros,"style_images/HMnStyle/user.gif");
strcat(zapros," HTTP/1.1\nHost: ");
strcat(zapros,"hmn.pp.ru");
strcat(zapros,"\nUser-agent: ");
strcat(zapros,"Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.8) Gecko/2009032609 )");
strcat(zapros,"\nAccept: */*\n\n");
if (!bConnected)
{
printf( "Cannot connected\n" );
return FALSE;
}
else
{
// Send the magic http request
if (send(hSocket, zapros, strlen(zapros), 0) == SOCKET_ERROR)
return FALSE;
fd_read.fd_array[0] = hSocket; fd_read.fd_count = 1;
fd_error.fd_array[0] = hSocket; fd_error.fd_count = 1;
char otvet[1024];
while (select(0, &fd_read, 0, &fd_error, &timeout) > 0)
{
if (fd_read.fd_count > 0)
{
int nNumRead = recv(hSocket, otvet, 1024, 0);
if (nNumRead <= 0)
break;
}
else
{
break;
}
}
}
closesocket(hSocket);
return TRUE;
}
Content-Type: image/gif
я знаю что сам контент файла начинается после Content-Type: image/gif
но как их разъединить, и еще, как сохранить в файл в буфер содержащий нули ?
#include<stdio.h>
#include<string.h>
#include<winsock2.h>
#include<windows.h>
#define HTTP_PORT 80
#define HTTP_IP "10.207.112.134"
//
// The Scan function
//
int main()
{
// This function does the actual scanning
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET hSocket;
// Initialize the socket
hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (hSocket == INVALID_SOCKET)
{
// Return FALSE in case of an error
return FALSE;
}
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = inet_addr ( HTTP_IP );;
sin.sin_family = AF_INET;
sin.sin_port = htons(HTTP_PORT);
fd_set fd_read, fd_write, fd_error;
timeval timeout;
timeout.tv_sec = 3; // TODO: Hardcoded, should be configurable
timeout.tv_usec = 0;
u_long nNonBlocking = 1;
// Set socket to non-blocking mode
ioctlsocket(hSocket, FIONBIO, &nNonBlocking);
BOOL bConnected = FALSE;
// Estabilish a TCP connection
connect(hSocket, (sockaddr*)&sin, sizeof(sin));
fd_write.fd_array[0] = hSocket; fd_write.fd_count = 1;
fd_error.fd_array[0] = hSocket; fd_error.fd_count = 1;
if (select(0, 0, &fd_write, &fd_error, &timeout) > 0)
{
if (fd_write.fd_count > 0)
{
// Connection successful
bConnected = TRUE;
}
}
char zapros[2048];
strcpy(zapros,"GET /");
strcat(zapros,"style_images/HMnStyle/user.gif");
strcat(zapros," HTTP/1.1\nHost: ");
strcat(zapros,"hmn.pp.ru");
strcat(zapros,"\nUser-agent: ");
strcat(zapros,"Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.8) Gecko/2009032609 )");
strcat(zapros,"\nAccept: */*\n\n");
if (!bConnected)
{
printf( "Cannot connected\n" );
return FALSE;
}
else
{
// Send the magic http request
if (send(hSocket, zapros, strlen(zapros), 0) == SOCKET_ERROR)
return FALSE;
fd_read.fd_array[0] = hSocket; fd_read.fd_count = 1;
fd_error.fd_array[0] = hSocket; fd_error.fd_count = 1;
char otvet[1024];
while (select(0, &fd_read, 0, &fd_error, &timeout) > 0)
{
if (fd_read.fd_count > 0)
{
int nNumRead = recv(hSocket, otvet, 1024, 0);
if (nNumRead <= 0)
break;
}
else
{
break;
}
}
}
closesocket(hSocket);
return TRUE;
}