http 文件上传例子

 

#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 300M chunks,最大支持300M文件
 BOOL bRead, bRet;
 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
 //使用put方式上传文件:
 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;
 }
 //打开指定的文件:
 HANDLE hFile = CreateFile(upFile, GENERIC_READ, FILE_SHARE_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);
  //结束一个HTTP请求:
 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"; //这里填写URL地址
 char *ServerName=argv[1];
 HINTERNET hSession = InternetOpen("HttpSendRequest",
 INTERNET_OPEN_TYPE_PRECONFIG,
 NULL,
 NULL,
 0); //同步方式
 if(!hSession)
 {
  printf("Failed to open InternetOpen\n");
  exit(0);
 }
 //连接到一个http服务:
 HINTERNET hConnect = InternetConnect(hSession,
 ServerName,
 INTERNET_DEFAULT_HTTP_PORT, //连接到80端口
 NULL,
 NULL,
 INTERNET_SERVICE_HTTP, //服务类型HTTP,FTP或Gopher
 0,
 1);
 if(!hConnect)
 {
  printf("error InternetConnect\n");
  return 0;
 }
 //TCHAR *putfile="d:\\1.exe"; //上传的程序
 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值