CHttpFile::EndRequest

fengrx 2009-07-17 10:06:03
在使用代理的情况下,使用POST协议上传文件,上传完成时,使用EndRequest结束请求时,异常“必须再次运行该操作”
再次运行一次程序就正常了。不知道为什么必须运行两次才能成功

我的代码:

if (FALSE == fTrack.Open(pThis->_mFilePath, CFile::modeRead | CFile::shareDenyWrite))//读出文件
{
pThis->strError += _T("Unable to open the file.");
return nSuccess;
}

strHTTPBoundary = _T("---------------------------7b4a6d158c9");//定义边界值
strPreFileData = pThis->MakePreFileData(strHTTPBoundary, pcmname);
strPostFileData = pThis->MakePostFileData(strHTTPBoundary);

dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();//计算整个包的总长度

dwChunkLength = 16 * 1024;
pBuffer = malloc(dwChunkLength);
if (NULL == pBuffer)
{
return nSuccess;
}

try
{

char strProxyList[MAX_PATH], strUsername[64], strPassword[64];
strcpy(strProxyList, "10.168.166.205:808");
strcpy(strUsername, "fengrongxing");
strcpy(strPassword, "123456");
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = strProxyList;
proxyinfo.lpszProxyBypass = NULL;
Session.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));

pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
//取得HttpFile对象

pHttpFile->SetOption(INTERNET_OPTION_PROXY_USERNAME, strUsername, strlen(strUsername)+1);
pHttpFile->SetOption(INTERNET_OPTION_PROXY_PASSWORD, strPassword, strlen(strPassword)+1);

pHttpFile->AddRequestHeaders(pThis->MakeRequestHeaders(strHTTPBoundary));//发送包头请求
pHttpFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);

pHttpFile->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());//写入服务器所需信息

dwReadLength = -1;
while (0 != dwReadLength)
{
dwReadLength = fTrack.Read(pBuffer, dwChunkLength);//文件内容
if (0 != dwReadLength)
{
pHttpFile->Write(pBuffer, dwReadLength);//写入服务器本地文件,用二进制进行传送
}
}
fTrack.Close();

pHttpFile->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
pHttpFile->EndRequest(HSR_SYNC);//此处异常*****************************此处异常


dwResponseLength = pHttpFile->GetLength();
while (0 != dwResponseLength)
{
szResponse = (LPSTR)malloc(dwResponseLength + 1);
szResponse[dwResponseLength] = '\0';
pHttpFile->Read(szResponse, dwResponseLength);
pThis->strResponse += szResponse;
free(szResponse);
dwResponseLength = pHttpFile->GetLength();
}

pHttpFile->Close();
delete pHttpFile;

DWORD dw ;
if (InternetQueryDataAvailable((HINTERNET)(*pHttpFile), &dw, 0, 0) && (dw == 0))
{
//设置成功标志
nSuccess = 0;
}
}
catch (CException* e)
{
TCHAR szCause[MAX_PATH] = {0};
e->GetErrorMessage(szCause, MAX_PATH);
e->Delete();
pThis->strError += szCause;

//删除http文件对象
if(pHttpFile != NULL ) delete pHttpFile;
//删除http连接对象
if(pHttpConnection != NULL) delete pHttpConnection;
//关闭http连接
Session.Close();
}

if (NULL != pBuffer)
{
free(pBuffer);
}
return nSuccess;


相关内容请查看帖子:http://topic.csdn.net/u/20090716/17/55969a50-7492-4700-a88a-700c58bd98f0.html
...全文
820 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwsn007 2009-07-18
  • 打赏
  • 举报
回复
此帖应该顶
wangk 2009-07-17
  • 打赏
  • 举报
回复
是不是代理服务器设定的问题,我的意思是代理服务器软件的设置问题?不是客户端代码的问题?
建议自己安装一个代理服务器,看看代理服务器Log信息。
fengrx 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tsing_best 的回复:]
不太清楚,最近也在搞winInet,不过还没试过代理的。。帮顶一个

[/Quote]

有成果后共享啊,,
hhwei1985 2009-07-17
  • 打赏
  • 举报
回复
up
MFCJCK 2009-07-17
  • 打赏
  • 举报
回复
不太清楚,最近也在搞winInet,不过还没试过代理的。。帮顶一个
wangk 2009-07-17
  • 打赏
  • 举报
回复
……
这样吧,去下载Microsoft Network Monitor或其他嗅探工具,用它截包看看你的应用程序你一次是否通过http把请求发出来了,格式是否正确。
biweilun 2009-07-17
  • 打赏
  • 举报
回复
use INTERNET_OPEN_TYPE_PROXY property in OpenRequest
fengrx 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangk 的回复:]
是不是代理服务器设定的问题,我的意思是代理服务器软件的设置问题?不是客户端代码的问题?
建议自己安装一个代理服务器,看看代理服务器Log信息。
[/Quote]

我在本机启动的CCPROXY,没有日志信息。
第一次运行,异常“必须再次运行该操作”
不关闭,直接运行第二次,成功,CCPROXY里边有日志信息。
如果关闭再重新启动运行的话,和第一次运行现象一致。

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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