请高手帮忙,上传出错啊,有代码

zjjhfwh 2006-06-14 05:25:21
DWORD dwHttpRequestFlags= INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_TRANSFER_BINARY;
CString csServer = "127.0.0.1";
INTERNET_PORT nPort = 80;

CInternetSession *pSession = new CInternetSession("upload");

CHttpConnection *pHttpConn = NULL;

pHttpConn = pSession->GetHttpConnection(csServer, nPort);

CHttpFile *pHttpFile = NULL;

CString csFile = "/aa.txt";
pHttpFile = pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_PUT, csFile, NULL, 1, NULL, NULL, dwHttpRequestFlags);
ASSERT( pHttpFile != NULL );

CFile File;
File.Open("C:\\aa.txt", CFile::modeRead);
int nLen = File.GetLength();
char *buffer = new char[nLen];
memset(buffer, 0, nLen);
File.Read(buffer, nLen);
File.Close();

/*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 = nLen; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;*/

//int res = pHttpFile->SendRequestEx(&BufferIn,NULL,HSR_INITIATE,0);
int res = pHttpFile->SendRequestEx(nLen);
if (res == 0)
MessageBox("send error.");
pHttpFile->Write(buffer, nLen);


pHttpFile->EndRequest();
pHttpFile->Close();
delete pHttpFile;
pHttpConn->Close();
delete pHttpConn;
pSession->Close();
delete pSession;

delete []buffer;
buffer = NULL;
...全文
204 点赞 收藏 7
写回复
7 条回复
huaxiaoyu 2006年08月24日
CString pURL;
CString SaveAsFilePath;
CString strRemoteFile;

pURL="http://192.168.0.18";
SaveAsFilePath="f:\\ts.txt";
strRemoteFile="ts.txt";



CInternetSession session; //会话期对象)
CHttpConnection* pServer = NULL; // 指向服务器地址(URL)
CHttpFile * pHttpFile = NULL;//HTTP文件指针


CString strServerName; //服务器名
CString strObject; //查询对象名(http文件)
INTERNET_PORT nPort; //端口
DWORD dwServiceType; //服务类型
DWORD dwHttpRequestFlags =INTERNET_FLAG_NO_AUTO_REDIRECT;//请求标志
BOOL OK=AfxParseURL( pURL, //被分析URL串
dwServiceType, //服务类型,ftp,http等
strServerName, //服务器名
strObject, //URL中被查询对象
nPort ); //URL指定的端口,可能为空
OK=OK && (dwServiceType ==INTERNET_SERVICE_HTTP);
if (!OK)
{
AfxMessageBox("URL出错"); //报错
return;
}

CString strData="THIS IS A TEST";
pServer = session.GetHttpConnection(strServerName, nPort,"admin","admin"); //获得服务器名
if(pServer == NULL)
{
AfxMessageBox("连接失败!");
return;
}
//向服务器发送请求,建立http连接,
pHttpFile = pServer-> OpenRequest(CHttpConnection::HTTP_VERB_PUT,
strRemoteFile,NULL, 1, NULL, NULL,dwHttpRequestFlags );

//pHttpFile->AddRequestHeaders("Content-Range");
if(pHttpFile == NULL)
{
AfxMessageBox("发送请求失败!");
return;
}

CFile myfile;
if( !myfile.Open(SaveAsFilePath, CFile::modeRead | CFile::typeBinary))
{
MessageBox("Unable to open file");
return;
}

int nLen = myfile.GetLength();
char *buffer = new char[nLen];
memset(buffer, 0, nLen);
myfile.Read(buffer, nLen);
myfile.Close();
pHttpFile->SendRequestEx(nLen);
pHttpFile->Write(buffer,nLen);
pHttpFile->EndRequest(0,NULL,1);

pHttpFile ->Close();
pServer ->Close();
if (pHttpFile != NULL)
delete pHttpFile;
if (pServer != NULL)
delete pServer;
session.Close();
AfxMessageBox("文件上传成功!");


我是这样做的,成功了,但是就是不能断点续传,你可以参考一下,最好把它做成断点续传的,再贴出来,或者发到我邮箱:huaxiaoyuly@163.com,或都加我的QQ:发给我:125519600,注明:csdn
回复 点赞
huaxiaoyu 2006年08月11日
帮顶,小弟也在做这个,但还是没有思路,搞好了一定要发一份给我喔,
huaxiaoyuly@163.com
回复 点赞
zjjhfwh 2006年06月15日
我要用http上传文件,可是调试不过,是不是代码有问题
回复 点赞
贵子潘 2006年06月15日
给了这么多分,却不把问题说得具体些,岂不可惜?
回复 点赞
xiangrujian 2006年06月14日
帮顶!
回复 点赞
DentistryDoctor 2006年06月14日
不知具体问题。
回复 点赞
WecanHuang 2006年06月14日
up
回复 点赞
发动态
发帖子
VC/MFC
创建于2007-09-28

7898

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告