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新开窗口,打开的页面则是正常的。
百思不得其解,就好象是偶数次调用时不知道发到什么地方去了。各位大侠有什么好建议?
解决方案 »
- 菜单的下划线,在设计阶段有,运行时候就没有了
- 现在多媒体通讯方面流行什么技术呢?
- VC中如何判断一网络地址是否真实存在
- 怎么把一个editbox置灰!~
- 为什么我得不到正确的时间?
- 请问有没有什么办法可以防止别人用文件监控器 监控自己读写过的文件
- CString 的Find函数是不是有问题?
- 另我头疼。。。如果正在debug一个程序,在推出调试状态时,会不会让调用的dll中的一个线程突然终止?如何能让线程运行完在退出?
- !!!!!!!!!!!!改错题!!!!!!!!!!!急
- 用什么函数能够取得CPU的负荷率?
- 对树型控件节点的增加,删除.100分!!!
- 只要是高手,只要有爱心,都可以进来看看。。。 。。。
http://support.microsoft.com/kb/177188/EN-US/
//if (m_httpSession)
// InternetCloseHandle(m_httpSession);
为什么注掉