45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:怎么样利用HTTP方式上传代码?

怎么样利用HTTP方式上传代码?

2016-08-25 12:05:19 来源:www.45fan.com 【

怎么样利用HTTP方式上传代码?

#include<afxdtctl.h>
#include<Windows.h>
#include<WinINet.h>
#include<stdio.h>
BOOLUseHttpSendReqEx(HINTERNEThRequest,DWORDdwPostSize,CStringstrLocalFile);
BOOLUpload(CStringbstrLocalFile,CStringbstrServerIP,CStringstrServerPort,CStringbstrRemoteFile);
#defineBUFFSIZE500

voidmain(intargc,char**argv)
{

if(argc<5)
{
printf("Usage:Bigpost<LocalFile><ServerIP><ServerPort><ReomteFile>/n");
printf("<LocalFile>isthelocalfiletoPOST/n");
printf("<ServerIP>istheserver'sIPtoPOSTto/n");
printf("<ServerPort>istheserver'sPorttoPOSTto/n");
printf("<ReomteFile>isthevirtualpathtoPOSTto/n");
exit(0);
}
Upload(argv[1],argv[2],argv[3],argv[4]);
}
BOOLUseHttpSendReqEx(HINTERNEThRequest,DWORDdwPostSize,CStringstrLocalFile)
{
DWORDdwRead;
BYTE*buffer;
printf("Localfile:%s/n",strLocalFile);
FILE*fLocal;
if((fLocal=fopen(strLocalFile,"rb"))==NULL){
printf("Can'topenthefile:%s,maybeitdoesn'texist!/n",strLocalFile);
returnfalse;
}
fseek(fLocal,0L,SEEK_END);
dwRead=ftell(fLocal);
rewind(fLocal);
buffer=(BYTE*)malloc(dwRead);
if(!buffer){
printf("notenoughmemory!/n");
returnfalse;
}
printf("lengthoffile:%d/n",dwRead);
dwRead=fread(buffer,1,dwRead,fLocal);
dwPostSize=dwRead;

INTERNET_BUFFERSBufferIn;
DWORDdwBytesWritten;
BOOLbRet;
BufferIn.dwStructSize=sizeof(INTERNET_BUFFERS);//Mustbesetorerrorwilloccur
BufferIn.Next=NULL;
BufferIn.lpcszHeader=NULL;
BufferIn.dwHeadersLength=0;
BufferIn.dwHeadersTotal=0;
BufferIn.lpvBuffer=NULL;
BufferIn.dwBufferLength=0;
BufferIn.dwBufferTotal=dwPostSize;//ThisistheonlymemberusedotherthandwStructSize
BufferIn.dwOffsetLow=0;
BufferIn.dwOffsetHigh=0;

if(!HttpSendRequestEx(hRequest,&BufferIn,NULL,0,0))
{
printf("ErroronHttpSendRequestEx%d/n",GetLastError());
returnFALSE;
}
bRet=TRUE;
if(bRet=InternetWriteFile(hRequest,buffer,dwPostSize,&dwBytesWritten))
printf("/r%dbytessent.",dwPostSize);
if(!bRet)
{
printf("/nErroronInternetWriteFile%lu/n",GetLastError());
returnFALSE;
}

if(!HttpEndRequest(hRequest,NULL,0,0))
{
printf("ErroronHttpEndRequest%lu/n",GetLastError());
returnFALSE;
}
fclose(fLocal);
free(buffer);
returnTRUE;
}

BOOLUpload(CStringstrLocalFile,CStringstrServerIP,CStringstrServerPort,CStringstrRemoteFile){
DWORDdwPostSize=0;
intintServerPort=atoi(strServerPort);
HINTERNEThSession=InternetOpen("HttpSendRequestEx",INTERNET_OPEN_TYPE_PRECONFIG,
NULL,NULL,0);
if(!hSession)
{
printf("Failedtoopensession/n");
returnFALSE;
}
HINTERNEThConnect=InternetConnect(hSession,strServerIP,intServerPort,
NULL,NULL,INTERNET_SERVICE_HTTP,NULL,NULL);
if(!hConnect){
printf("Failedtoconnect/n");
returnFALSE;
}else{
HINTERNEThRequest=HttpOpenRequest(hConnect,"PUT",strRemoteFile,
NULL,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,0);
if(!hRequest){
printf("Failedtoopenrequesthandle/n");
}else{
if(UseHttpSendReqEx(hRequest,dwPostSize,strLocalFile))
{
charpcBuffer[BUFFSIZE];
DWORDdwBytesRead;

printf("/nThefollowingwasreturnedbytheserver:/n");
do
{dwBytesRead=0;
if(InternetReadFile(hRequest,pcBuffer,BUFFSIZE-1,&dwBytesRead))
{
pcBuffer[dwBytesRead]=0x00;//Null-terminatebuffer
printf("%s",pcBuffer);
}
else
printf("/nInternetReadFilefailed");
}while(dwBytesRead>0);
printf("/n");
}
if(!InternetCloseHandle(hRequest))
printf("FailedtocloseRequesthandle/n");
}
if(!InternetCloseHandle(hConnect))
printf("FailedtocloseConnecthandle/n");
}
if(InternetCloseHandle(hSession)==FALSE){
printf("FailedtocloseSessionhandle/n");
returnFALSE;
}
printf("/nFinished./n");
returnTRUE;
}


本文地址:http://www.45fan.com/a/question/67421.html
Tags: 上传 方式 HTTP
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部