利用HTTP方式上传

#include <afxdtctl.h>
#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile);
BOOL Upload(CString bstrLocalFile,CString bstrServerIP,CString strServerPort,CString bstrRemoteFile);
#define BUFFSIZE 500

void main( int argc, char **argv )
{

    if (argc < 5)
    {
        printf("Usage: Bigpost <LocalFile> <ServerIP><ServerPort><ReomteFile>/n");
        printf("<LocalFile> is the local file to POST/n");
        printf("<ServerIP> is the server's IP to POST to/n");
        printf("<ServerPort> is the server's Port to POST to/n");
        printf("<ReomteFile> is the virtual path to POST to/n");
        exit(0);
    }
    Upload(argv[1],argv[2],argv[3],argv[4]);
}
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,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_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, &dwBytesWritten))
            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 strLocalFile,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(UseHttpSendReqEx(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值