HTTP file download class source code

xiaoxiao2021-03-06  102

Attachment: TCP.H, TCP.CPP // httpclient.h: Implementation class / / / /

#if! defined (_httpclient_h __) # Define _httpclient_h__

#include "../commmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

#pragma Warning (Disable: 4786)

#include #include Using Namespace STD;

#define buffer_size 8192 # Define heads_buffer_size 2048 # Define peek_size 500 # define TCP_TIMEOUT 5

Class ChttpClient {public: void cancel (); const char * getStatusString (); chttpclient (); virtual ~ chttpclient ();

Bool Download (Char * URL, Char * FNAME); / / Download Download Download (Char * URL, HWND HPARENT, LONG MSG = WM_USER 1); // Message Download Data Stream

Inline Long Instr (Char * from, Char * Find); Virtual Void OnDatarecived (Char * Data, Long DataSize, INT First = False); Long GeTHostName (Char * URL, Char * BUF); int GetStatus (); long getHeader char * headname, char * buf); long GetHeader (char * from, char * searchfor, char * buf); bool Reset (); private: long m_Message; HWND m_hParent; bool m_noCancel; char m_fname [BUFFER_SIZE]; int m_status; Char m_heads_buffer [heads_buffer_size]; char m_buffer [buffer_size 1]; CTCP TCP;

Map M_Map;

#ENDIF / / ---------------------------------------------- ------------------ // httpClient.cpp: Implementation of the chttpclient class.////

#include "stdafx.h" #include "httpclient.h"

#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif

//// construction / destruction //

ChttpClient :: chttpclient () {reset (); Memset (m_fname, 0, buffer_size); tcp.init (); m_map [100] = "continue"; m_map [101] = "switching protocols"; m_map [200] = "Ok"; m_map [201] = "created"; m_map [202] = "accepted"; m_map [203] = "non-authoritative information"; m_map [204] = "no content"; m_map [205] = " Reset content "; m_map [206] =" partial content "; m_map [300] =" multiple choices "; m_map [301] =" moved permanently "; m_map [302] =" found "; m_map [303] =" See "; M_map [304] =" not modified "; m_map [305] =" use proxy "; m_map [307] =" Temporary redirect "; m_map [400] =" Bad Request "; m_map [401] =" unauthorized "; M_map [402] =" payment required "; m_map [403] =" forbidden "; m_map [404] =" not found "; m_map [405] =" method not allowed "; M_MAP [406] =" not acceptable "; M_map [407] =" proxy authentication required "; m_map [408] =" request time-out "; m_map [409] =" conflict "; m_map [410] =" gone "; m_map [411] =" Length "; M_map [412] =" precondition failed "; m_map [413] =" Request Entity Too Large "; M_MAP [414] =" Request-Uri Too Large "; M_map [415] = "unsupported Media Type"; m_map [416] = "Requested Range Not Satisfiabl"; m_map [417] = "expection failed"; m_map [500] = "interNal server error"; m_map [501] = " NOT IMPLEMENTED "; M_MAP [502] =" BAD GATEWAY "; M_MAP [503] =" service unavailable "; m_map [504] =" Gateway Time-Out ";

M_map [505] = "http version not supported";} chttpclient :: ~ chttpclient () {

}

Bool ChttpClient :: RESET () {m_nocancel = true; m_hparent = null; m_message = 0; MEMSET (M_Buffer, 0, Buffer_Size); MEMSET (m_heads_buffer, 0, heads_buffer_size); return 1;}

// http Download Main Program Bool ChttpClient :: Download (Char * URL, HWND HPARENT, Long MSG) {m_mentage = m_mentage; m_hparent = hparent;

// State is busy

Reset (); char hostname [256]; MEMSET (Hostname, 0,256); gethostname (URL, Hostname);

Long allsize = 0; long readsize = 0; bool headfinished = false;

INT S; S = TCP.Connect (Hostname, 80, TCP_TIMEOUT, 1); IF (S <0) Return False;

Sprintf (m_buffer, "get% s http / 1.0 / r / nhost:% s / r / naccept: * / * / r / nuser-agent: autOUpdate / 1.0 / r / n / r / n", URL, HostName) TCP.send (S, M_Buffer, Strlen (M_Buffer), TCP_TIMEOUT;

// Take the peek_size one byte so that you can take the content-length readsize = tcp.recv (s, m_buffer, peek_size, tcp_timeout); if (readsize == - 1) Return False; m_buffer [readsize] = 0;

MEMSET (Hostname, 0,256);

INT M, N; M = INSTR (m_buffer, "); n = INSTR (M_Buffer M 1," "); MEMCPY (Hostname, M_Buffer M 1, N);

m_status = atoi (HostName); if (m_status! = 200) Return False;

Memset (Hostname, 0,256); IF (GetHeader (M_Buffer, "Content-Length", Hostname <0) Return False;

// Get the content of the content allsize = atol (Hostname); MEMSET (Hostname, 0,256);

// Look for the HTTP data area to represent int I; i = INSTR (m_buffer, "/ r / n / R / n"); if (i> 0) {memcpy (m_heads_buffer, m_buffer, i 4); HeadFinished = true; Readsize = readsize-i-4; OnDatarecived (M_Buffer i 4, Readsize, True);

while (ReadSize BUFFER_SIZE BUFFER_SIZE:? (AllSize-ReadSize); memset (m_buffer, 0, BUFFER_SIZE); if (tcp.recv ( ! s, m_buffer, nSize, TCP_TIMEOUT) = nSize) return false; readSize = nSize; m_buffer [nSize] = 0; OnDataRecived (m_buffer, nSize);} else {nSize = (AllSize-readSize)> (BUFFER_SIZE-readSize)? (BUFFER_SIZE-readSize) :( AllSize-readSize); if (tcp.recv (s, m_buffer readSize, nSize, TCP_TIMEOUT) = nSize) return false;! readSize = nSize; m_buffer [nSize] = 0; // find HTTP Data area represents I = INSTR (M_Buffer, "/ R / N / R / N); if (i> 0) {memcpy (m_heads_buffer, m_buffer, i 4); HeadFinished = true; readsize = readsize-i-4 OnDatarecived (M_Buffer i 4, ReadSize, True);}}}}}}}}} Remove (M_FNAME);

TCP.Close (s); return true;}

INT ChttpClient :: getStatus () {return m_status;}

// Take out the host name from the URL Long ChttpClient :: gethostname (Char * URL, Char * BUF) {INT I, J; Char * LPSZ = _TCSSTR (URL, "//"); if (! Lps) return -1 I = LPSZ-URL 2;

LPSZ = _TCSSTR (URL I, "/"); if (! lps) return -1; j = lpsz-url; memcpy (buf, URL I, J-I); Return J-I;

Long ChttpClient :: GetHeader (Char * ", char * buf) {Return GetHeader (m_heads_buffer, headname, buf);

Long ChttpClient :: GetHeader (char * from, char *) {char * p; char * LPSZ = _TCSSTR (from, headname); if (! lps) return -1;

// LPSZ ; P = LPSZ Strlen (HeadName) 2; LPSZ = _TCSSTR (p, "/ r / n");

IF (! lpsz) return -1;

Memcpy (BUF, P, LPSZ-P);

Return Strlen (BUF);

Void ChttpClient :: Ondatarecived (Char * Data, Long DataSize, int first) {

IF (m_hparent) :: SendMessage (m_hparent, m_message, (unsigned int) DATA, DATASIZE); ELSE {if (first) remote (m_fname); file * f = fopen (m_fname, "ab"); if (f) { FWRITE (DATA, 1, DATASIZE, F); Fclose (f);}} // Handling data, must copy, buffer is used} inline long chttpclient :: instr (char * from, char * find ) {Char * LPSZ = _tcsstr (from, find); Return (! Lps)? - 1: lpsz-from;

Bool ChttpClient :: Download (Char * URL, Char * FNAME) {STRCPY (M_FName, FNAME); Return Download (URL, NULL, NULL);}

Const char * ChttpClient :: getStatusString () {RETURN M_MAP [M_STATUS] .c_str ();

Void ChttpClient :: Cancel () {m_nocancel = false;}

转载请注明原文地址:https://www.9cbs.com/read-104198.html

New Post(0)