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新开窗口,打开的页面则是正常的。
百思不得其解,就好象是偶数次调用时不知道发到什么地方去了。各位大侠有什么好建议?
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新开窗口,打开的页面则是正常的。
百思不得其解,就好象是偶数次调用时不知道发到什么地方去了。各位大侠有什么好建议?
解决方案 »
- 求:音乐播放器思路 或代码
- 多线程,多媒体定时器的效率
- 有关directshow缓冲问题
- 单文档形式的应用中,点击菜单命令在哪个类中响应比较合适?(小声说:我是菜鸟)
- DCOM下怎么使用MSCOMM控件啊。
- 如何在派生自CSrollView类的类中添加代码使其生成的程序能输入字?
- 我的程序在2k和xp下运行没问题,在98下出问题了,求救
- 请问一下, VC做视频开发涉及到了哪些标准哦??
- 哪有WINDOWS2000 可以下载啊?急………………………………,没分了,现在,以后再补,谢谢,(111222)
- 给个连接mysql 网站的代码
- 对树型控件节点的增加,删除.100分!!!
- 只要是高手,只要有爱心,都可以进来看看。。。 。。。
http://support.microsoft.com/kb/177188/EN-US/
//if (m_httpSession)
// InternetCloseHandle(m_httpSession);
为什么注掉