怪问题,控件HTTP上传方法执行1,3,5,7....次成功,第2,4,6,8...次失败

jiayuanliao 2005-07-29 11:13:18
VC++ ATL编写的控件在执行HTTP上传时奇数次成功,偶数次失败,主代码如下:

{
m_httpSession = ::InternetOpen(httpSession_Agent_name,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
_variant_t vttSvrName;
vttSvrName = pwszSvrName;
TCHAR *svrName;
svrName= _com_util::ConvertBSTRToString(vttSvrName.bstrVal);
// 连接服务器
HINTERNET hConnect = InternetConnect(m_httpSession,
svrName,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
1);
if (!hConnect) return E_UNEXPECTED;
LPWSTR pwszHDSTemp = L"Content-Type: application/x-www-form-urlencoded\nAccept: */*\nAccept-Encoding: gzip, deflate\nAccept-Language: zh-cn\nCookie: ASP.NET_SessionId=";
LPWSTR pwszHDS = NULL;
pwszHDS = DsoCopyStringCat(pwszHDSTemp,pwszSessionID);
LPWSTR pwszFullTargetUrl = NULL;
pwszFullTargetUrl = DsoCopyString(pwszTargetUrl);
pwszFullTargetUrl = DsoCopyStringCat(pwszFullTargetUrl,L"?");
pwszFullTargetUrl = DsoCopyStringCat(pwszFullTargetUrl,pwszUrlHead);
_variant_t vttTargetUrl, vttHDS;
vttTargetUrl = pwszFullTargetUrl;
vttHDS = pwszHDS;

TCHAR *buf;
TCHAR *hdrs;
buf = _com_util::ConvertBSTRToString(vttTargetUrl.bstrVal);
hdrs= _com_util::ConvertBSTRToString(vttHDS.bstrVal);
static TCHAR accept[] = _T("Accept: */*");

HINTERNET hRequest = HttpOpenRequest(hConnect,
"POST",
buf,
HTTP_VERSION,
NULL,
NULL,
0,
1);
if (!hRequest) return E_UNEXPECTED;
HttpAddRequestHeaders(hRequest,hdrs,lstrlen(hdrs),HTTP_ADDREQ_FLAG_ADD);
INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
BYTE pBuffer[1024];
BOOL bRet, bRead;

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.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;
DWORD dwPostSize = ::GetFileSize(hFile,NULL);
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize

if(!(hr=HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0)))
{
return hr;
}
while (SUCCEEDED(hr))
{
if (FALSE == ReadFile(hFile, pBuffer, 1024, &cbRead, NULL))
{
hr = E_WIN32_LASTERROR;
break;
}
if (0 == cbRead) break;
if (!(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten))){
hr = E_UNEXPECTED;
break;
}
}
CloseHandle(hFile);
MessageBox(m_hwnd,
"即将关闭Request",
"调试", MB_ICONINFORMATION|MB_SETFOREGROUND);
if(!HttpEndRequest(hRequest, NULL, 0, 0)){
return E_UNEXPECTED;
}
MessageBox(m_hwnd,
"关闭Request成功",
"调试", MB_ICONINFORMATION|MB_SETFOREGROUND);
char pcBuffer[BUFFSIZE];

DWORD dwBytesRead;
{
dwBytesRead=0;
bRead = InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1, &dwBytesRead);
if (bRead){
MessageBox(m_hwnd,
pcBuffer,
"文件上传结果", MB_ICONINFORMATION|MB_SETFOREGROUND);
}
}
// 清除句柄
if (hRequest)
InternetCloseHandle(hRequest);
if (hConnect)
InternetCloseHandle(hConnect);
//if (m_httpSession)
// InternetCloseHandle(m_httpSession);
}


返回信息:(没有异常发生,但返回的东西不对,并且在Web应用程序一端没跟踪到数据提交)
<html><head><title>Error</title></head><body>The parameter is incorrect. </body></html>

测试范围:ActiveX Control Test Container和IE嵌入都有这个问题。

在用IE进行测试时,当我上传成功后,按Ctrl+N新开窗口,打开的页面的源代码就是:
<html><head><title>Error</title></head><body>The parameter is incorrect. </body></html>

但如果我上传失败后,按Ctrl+N新开窗口,打开的页面则是正常的。
百思不得其解,就好象是偶数次调用时不知道发到什么地方去了。

各位大侠有什么好建议?
...全文
139 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodboyws 2005-09-23
  • 打赏
  • 举报
回复

//if (m_httpSession)
// InternetCloseHandle(m_httpSession);
为什么注掉
duyhui 2005-09-23
  • 打赏
  • 举报
回复
你上微软的网站上看看,有个上传的例子.
http://support.microsoft.com/kb/177188/EN-US/
findingit 2005-07-29
  • 打赏
  • 举报
回复
代码肯定有错,调试一下看看出错在哪一行。我就有过一样的错误方式,多数是因为没有清空旧的数据所造成的,你看看哪行的数据会受上次数据的影响,将它清空再加值,这样就不会错了,祝你好运!

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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