HTTP Server Download file

Source: Internet
Author: User

header files and library files to include

#include <string>

#include <Wininet.h>

#include <fstream>

#pragma comment (lib, "Wininet.lib")

1. Open the server

int Chttp::createconnect (const string & httpserver,const String & username,const string &password,const int n Port)

{

DWORD err;
M_session = NULL;
M_connect = NULL;

Hinternet m_session = InternetOpen (L "AGENT", internet_open_type_preconfig,null,null,0);

if (m_session!) = NULL)

{

if (Httpserver.find ("www")!=string::npos)
{
M_connect = Internetconnecta (M_session, Httpserver.c_str (), Nport,username.c_str (), Password.c_str (), INTERNET_ service_http,0,0);
}
Else
{
M_connect = Internetconnecta (M_session, Httpserver.c_str (), Internet_default_http_port,username.c_str (), PASSWORD.C _str (), internet_service_http,0,0);
}

}

if (M_connect = = NULL)
{
Err = GetLastError ();

return op_fail;
}

return op_success;
}

2. Download the file

int chttp::D ownloadfile (const wstring & remotefilename,const wstring & Localpath,const wstring & LocalFilename)
{
if (M_connect = = NULL)
{
return op_fail;
}

Hinternet m_website = HttpOpenRequest (m_connect,l "GET", Remotefilename.c_str (), Http_version,null,0,internet_flag_ dont_cache| internet_flag_reload,0);


if (M_website = = NULL)
{
DWORD err = GetLastError ();

return op_fail;
}

BOOL bsendrequest=::httpsendrequest (m_website,null,0,0,0);

wchar_t achquerybuf[1024];

DWORD Dwquerybuflen = sizeof (ACHQUERYBUF);

BOOL Bquery=::httpqueryinfo (m_website,http_query_status_code,achquerybuf,&dwquerybuflen,null);

wstring str = ACHQUERYBUF;

if (m_pinitstruct! = NULL)
{
wchar_t querybuf[1024];
DWORD Querybuflen = sizeof (QUERYBUF);
BOOL Blquery=::httpqueryinfo (m_website,http_query_content_length,querybuf,&querybuflen,null);

wstring Buff = querybuf;
int length = _wtoi (Buff.c_str ());
}
if (!bquery)
{

return op_fail;
}

if (str = = L "$" | | str = = L "302")
{
Temporary file name

wstring tempfile = localpath+l\\package.tmp;

DeleteFile (Tempfile.c_str ());

FILE *fp_file = NULL;
DWORD err = _wfopen_s (&fp_file,tempfile.c_str (), L "WB");

DWORD number = 1;

DWORD maxblocksize = 512;
Char temp[512];

while (number)
{
memset (temp,0,512);

             if (! InternetReadFile (m_website,temp,maxblocksize-1,&number))
              {
                  fclose (Fp_file);
                 return Op_ FAIL;
            }
    
             fwrite (Temp, sizeof (char), number, fp_file);
         }
  
          fclose (fp_file);

Rename to target file
wstring desfile = LocalPath;

DeleteFile (Desfile.c_str ());

:: MoveFile (Tempfile.c_str (), Desfile.c_str ());

return op_success;
}
Else
{
return op_fail;
}
}

3. Shutting down the server

int Chttp::close ()
{
if (m_website! = NULL)
{
InternetCloseHandle (M_website);
}
if (m_connect! = NULL)
{
InternetCloseHandle (M_connect);
}

if (m_session! = NULL)
{
InternetCloseHandle (m_session);
}

return op_success;
}

HTTP Server Download file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.