Using HTTP mode upload (code)

xiaoxiao2021-03-06  41

#include

#include

#include

#include

Bool Usehttpsendreqex (Hinternet HREQUEST, DWORD DWPOSTSI, CSTRING STRLOCALFILE);

Bool Upload (CString BstrSerFile, CString BSTRSERVERIP, CSTRING STRSERVERPORT, CSTRING BSTRREMOTEFILE);

#define buffsize 500

Void main (int Argc, char ** argv)

{

IF (Argc <5)

{

Printf ("Usage: BigPost / n");

Printf (" Is The Local File to POST / N");

Printf (" Is The Server's IP to Post To / N");

Printf (" is the server's port to post to / n");

Printf (" Is The Virtual Path TO POST TO / N);

exit (0);

}

Upload (Argv [1], Argv [2], Argv [3], Argv [4]);

}

Bool Usehttpsendreqex (Hinternet HREQUEST, DWORD DWPOSTSI, CSTRING STRLOCALFILE)

{

DWORD DWREAD;

BYTE * BUFFER;

Printf ("Local File:% S / N", strlocalfile;

File * flocal;

IF ((FLOCAL = FOPEN (StrlocalFile, "RB")) == null) {

Printf ("Can't Open THE FILE:% S, Maybe It Doesn't Exist! / N", strlocalfile;

Return False;

}

FSeek (Flocal, 0L, seek_ek_end);

dwread = ftell (flocal);

Rewind (flocal);

Buffer = (Byte *) Malloc (dwread);

IF (! buffer) {

Printf ("Not Enough Memory! / N");

Return False;

}

Printf ("Length of File:% D / N", DWREAD);

Dwread = FREAD (Buffer, 1, Dwread, Flocal);

DWPOSTSIZE = dwread;

Internet_buffers bufferin;

DWORD DWBYTESWRITTEN

BOOL BRET;

Bufferin.dwstructSize = sizeof (Internet_buffers); // Must Be set Or Error Will Occur

Bufferin.next = NULL;

Bufferin.lpcszheader = null;

Bufferin.dwheadersLength = 0;

Bufferin.dwheaderstotal = 0; bufferin.lpvbuffer = null;

Bufferin.dwbufferLength = 0;

Bufferin.dwbuffertotal = dwpostsize; // this is the only member used Other Than DWSTRUCTSIZE

Bufferin.dwoffsetlow = 0;

Bufferin.dwoffsethiGH = 0;

IF (! httpsendrequestex (hRequest, & bufferin, null, 0, 0))

{

Printf ("ERROR ON HTTPSENDREQUESTEX% D / N", getLastError ());

Return False;

}

Bret = True;

IF (Bret = InternetWritefile (HREQUEST, BUFFER, DWPOSTSIZE, & DWBYTESWRITEN)

Printf ("/ R% D Bytes Sent.", DWPOSTSIZE);

IF (! bret)

{

Printf ("/ NERROR On InternetWriteFile% lu / n", getLastError ());

Return False;

}

IF (! httpendrequest (hRequest, null, 0, 0))

{

Printf ("ERROR ON HTTPENDREQUEST% lu / n", getLastError ());

Return False;

}

Fclose (FLOCAL);

Free (buffer);

Return True;

}

Bool Upload (CString StrserveriP, CString StrserverPort, Cstring Strremotefile) {

DWORD dwpostsize = 0;

INT INTSERVERPORT = ATOI (STRSERVERPORT);

Hinternet hsession = Internetopen ("httpsendrequestex", internet_open_type_preconfig,

NULL, NULL, 0);

IF (! hsession)

{

Printf ("Failed to Open Session / N");

Return False;

}

Hinternet hconnect = InternetConnect (HSession, STRSERVERIP, INTSERVERPORT,

NULL, NULL, Internet_Service_HTTP, NULL, NULL;

IF (! hconnect) {

Printf ("Failed to Connect / N);

Return False;

} else {

Hinternet hRequest = httpopenRequest (hconnect, "put", strremotefile,

NULL, NULL, NULL, Internet_flag_no_cache_write, 0);

IF (! hRequest) {

Printf ("Failed to Open Request Handle / N");

} else {

IF (Userttpsendreqex (HREQUEST, DWPOSTSIZE, STRLOCALFILE)

{

Char pcbuffer [buffsize];

DWORD DWBYTESREAD;

Printf ("/ NTHE FOLLOWING WAS RETURNED BY THE Server: / N"); Do

{Dwbytesread = 0;

IF (InternetReadFile (HREQUEST, PCBUFFER, BUFFSIZE-1, & DWBYTESREAD))

{

PCBuffer [dwbytesread] = 0x00; // Null-Terminate Buffer

Printf ("% s", pcbuffer;

}

Else

Printf ("/ NinternetReadFile Failed");

} While (dwbytesread> 0);

Printf ("/ n");

}

IF (! InternetCloseHandle (HREQUEST))

Printf ("Failed to Close Request Handle / N");

}

IF (! InternetCloseHandle (HConnect))

Printf ("Failed to Close Connect Handle / N);

}

IF (InternetCloseHandle (HSESSION) == false) {

Printf ("Failed to Close Session Handle / N");

Return False;

}

Printf ("/nfinished./n");

Return True;

}

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

New Post(0)