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

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;
...全文
223 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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