Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   С/С++, C#, Delphi, .NET, Asm (https://forum.antichat.xyz/forumdisplay.php?f=24)
-   -   С++ скачивание файлов по http (https://forum.antichat.xyz/showthread.php?t=115233)

fire64 10.04.2009 12:53

С++ скачивание файлов по http
 
Каким образом отделить файл от заголовка http ?

Content-Type: image/gif

я знаю что сам контент файла начинается после Content-Type: image/gif

но как их разъединить, и еще, как сохранить в файл в буфер содержащий нули ?


PHP код:

#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_INETSOCK_STREAMIPPROTO_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_readfd_writefd_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(hSocketFIONBIO, &nNonBlocking);

    
BOOL bConnected FALSE;

    
// Estabilish a TCP connection
    
connect(hSocket, (sockaddr*)&sinsizeof(sin));

    
fd_write.fd_array[0] = hSocketfd_write.fd_count 1;
    
fd_error.fd_array[0] = hSocketfd_error.fd_count 1;
    if (
select(00, &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(hSocketzaprosstrlen(zapros), 0) == SOCKET_ERROR)
            return 
FALSE;


        
fd_read.fd_array[0] = hSocketfd_read.fd_count 1;
        
fd_error.fd_array[0] = hSocketfd_error.fd_count 1;

        
char otvet[1024];

        while (
select(0, &fd_read0, &fd_error, &timeout) > 0)
        {
            if (
fd_read.fd_count 0)
            {
                
int nNumRead recv(hSocketotvet10240);
                
                if (
nNumRead <= 0)
                    break;
            }
            else
            {
                break;
            }
        }

    }

    
closesocket(hSocket);    

    return 
TRUE;



razb 10.04.2009 13:31

Цитата:

Каким образом отделить файл от заголовка http ?
я знаю что сам контент файла начинается после Content-Type: image/gif
Поиском по строке идешь до Content-Type: image/gif, а начиная с позиции за ним копируешь memcpy() куда тебе надо.
Цитата:

как сохранить в файл в буфер содержащий нули ?
Немного вопрос непонятен, из буфера в файл? тогда fwrite();


Время: 11:17