怎样使用Wininet库将文件上传到FTP服务器?

gdi_gtk2 2008-04-24 09:54:47
假如用户名:name
密码:password
文件:1.txt
FTP目录:doc
...全文
71 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kudeet 2008-04-24
  • 打赏
  • 举报
回复
然后是cpp文件



#include "StdAfx.h"
#include ".\uploadfile.h"

UpLoadFile::UpLoadFile(void)
{
}

UpLoadFile::~UpLoadFile(void)
{
}

BOOL UpLoadFile::UseHttpSendReqEx(CHttpFile* httpFile, DWORD dwPostSize,CString strLocalFile)
{
try
{
DWORD dwRead,dwRet;

BYTE* buffer;
TRACE("Local file:%s\n",strLocalFile);
FILE* fLocal;
if((fLocal=fopen(strLocalFile,"rb"))==NULL)
{
TRACE("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){
TRACE("not enough memory!\n");
return false;
}

TRACE("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;

httpFile->SendRequestEx(&BufferIn,NULL,HSR_INITIATE,0);
//httpFile->SendRequestEx(dwPostSize);

httpFile->Write( buffer, dwPostSize);

if(!httpFile->EndRequest(0,0,0))
{
TRACE( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}

fclose(fLocal);
free(buffer);
return TRUE;
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
}
return FALSE;
}


BOOL UpLoadFile::Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile)
{
try
{
DWORD dwPostSize=0;
INTERNET_PORT intServerPort=atoi(strServerPort);

CInternetSession internetSession("my session");


CHttpConnection* httpConnection = internetSession.GetHttpConnection(strServerIP,intServerPort);
if(httpConnection == NULL)
{
TRACE( "Failed to connect\n" );
return FALSE;
}

CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
//CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile);
if(httpFile == NULL)
{
TRACE( "Failed to open request handle\n" );
return FALSE;
}

if(UseHttpSendReqEx(httpFile, dwPostSize,strLocalFile))
{

TRACE( "\nSend Finished.\n" );

httpFile->Close();
httpConnection->Close();
internetSession.Close();
return TRUE;
}

httpFile->Close();
httpConnection->Close();
internetSession.Close();
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
}
return FALSE;

}
Kudeet 2008-04-24
  • 打赏
  • 举报
回复
ZT:
下面是我写的一个类,用http put协议上传文件的
首先是头文件


#pragma once
#include <afxinet.h>

class UpLoadFile
{
public:
UpLoadFile(void);
~UpLoadFile(void);

BOOL Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile);
private:
BOOL UseHttpSendReqEx(CHttpFile* httpFile,DWORD dwPostSize,CString strLocalFile);
};
gdi_gtk2 2008-04-24
  • 打赏
  • 举报
回复
详细些好吗?
hblinlin 2008-04-24
  • 打赏
  • 举报
回复
自己封装一个简单ftp功能类吧
caixingxin 2008-04-24
  • 打赏
  • 举报
回复
CInternetSession,CFtpConnection
查一下这两个东西的用法~~~

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧