Http File Upload example

Source: Internet
Author: User
Tags http file upload

 

# Include <winsock2.h>
# Include <windows. h>
# Include <stdio. h>
# Include <wininet. h>
# Pragma comment (Lib, "ws2_32.lib ")
# Pragma comment (Lib, "wininet. lib ")

Bool usehttpsendreqex (hinternet hconnect, tchar * upfile, tchar * localfile)
{
Internet_buffers Bufferin = {0 };
DWORD dwbytesread;
DWORD dwbyteswritten;
Byte pbuffer [302480]; // read from file in 300 m chunks, up to M files supported
Bool bread, Bret;
Bufferin. dwstructsize = sizeof (internet_buffers );
// Use put to upload files:
Hinternet hrequest = httpopenrequest (hconnect,
"Put ",
Localfile,
Null,
Null,
Null,
Internet_flag_no_cache_write,
0 );
If (! Hrequest)
{
Printf ("failed to open request handle: % lu \ n", getlasterror ());
Return false;
}
// Open the specified file:
Handle hfile = createfile (upfile, generic_read, file_assist_read,
Null, open_existing, file_attribute_normal, null );
If (hfile = invalid_handle_value)
{
Printf ("\ nfailed to open local file % S.", upfile );
Return false;
}
Bufferin. dwbuffertotal = getfilesize (hfile, null );
Printf ("file size is % d \ n", Bufferin. dwbuffertotal );
If (! Httpsendrequestex (hrequest, & Bufferin, null, hsr_initiate, 0 ))
{
Printf ("error on httpsendrequestex % lu \ n", getlasterror ());
Return false;
}
DWORD sum = 0;
Do
{
If (! (Bread = readfile (hfile, pbuffer, sizeof (pbuffer), & dwbytesread, null )))
{
Printf ("\ nreadfile failed on buffer % lu.", getlasterror ());
Break;
}
If (! (Bret = internetwritefile (hrequest, pbuffer, dwbytesread, & dwbyteswritten )))
{
Printf ("\ ninternetwritefile failed % lu", getlasterror ());
Break;
}
Sum + = dwbyteswritten;
}
While (dwbytesread = sizeof (pbuffer ));
Closehandle (hfile );
Printf ("actual written Bytes: % d \ nupload % s successed! \ N ", sum, localfile );
// End an HTTP request:
If (! Httpendrequest (hrequest, null, 0, 0 ))
{
Printf ("error on httpendrequest % lu \ n", getlasterror ());
Return false;
}
Return true;
}

Int main (INT argc, char ** argv)
{
// Put 127.0.0.1/2.db D :\\ 1.exe
If (argc! = 4)
{
Printf ("Param num not correct. \ n ");
Return 0;
}
// Char * servername = "127.0.0.1"; // enter the URL here
Char * servername = argv [1];
Hinternet hsession = internetopen ("httpsendrequest ",
Internet_open_type_preconfig,
Null,
Null,
0); // synchronous Mode
If (! Hsession)
{
Printf ("failed to open internetopen \ n ");
Exit (0 );
}
// Connect to an HTTP service:
Hinternet hconnect = internetconnect (hsession,
Servername,
Internet_default_http_port, // connect to port 80
Null,
Null,
Internet_service_http, // Service Type HTTP, FTP or gopher
0,
1 );
If (! Hconnect)
{
Printf ("error internetconnect \ n ");
Return 0;
}
// Tchar * putfile = "D: \ 1.exe"; // The uploaded Program
Tchar * putfile = argv [3];
Tchar * putlocalfile = argv [2];
Bool sigh;
Sigh = usehttpsendreqex (hconnect, putfile, putlocalfile );
If (! Sigh)
{
Printf ("error usehttpsendreqex \ n ");
Return 0;
}
Return 0;
}

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.