你好,我有一个问题,下面这段代码,使用WinInet,通过代理取Google首页的数据,在MBCS下可以正常通过,UNICODE下却不行,请问是什么原因? TCHAR szProxy[MAX_PATH]=_T("127.0.0.1:9666");
CInternetSession session;
CHttpConnection* pConnection = NULL;
CHttpFile* pHttpFile = NULL;
DWORD dwStart = GetTickCount();
DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
CString strServer;
CString strObject;
INTERNET_PORT nPort;
AfxParseURL( _T("http://www.google.com/"),dwServiceType,strServer,strObject,nPort );
CString strAllData;
BOOL bInvalid = FALSE;
try
{
pConnection = session.GetHttpConnection(strServer,INTERNET_FLAG_KEEP_CONNECTION,INTERNET_INVALID_PORT_NUMBER,NULL, NULL);
pHttpFile = pConnection->OpenRequest(_T("GET"), strObject,NULL, 0, NULL, NULL,INTERNET_FLAG_KEEP_CONNECTION);
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = szProxy;
proxyinfo.lpszProxyBypass = NULL;
session.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000 * 10);
session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,1000 * 20);
pHttpFile->SendRequest(NULL);
DWORD nFileSize = pHttpFile->GetLength();
char *rBuffer = new char[nFileSize];
int nRead = pHttpFile->Read(rBuffer, nFileSize);
pHttpFile->Close();
strAllData = rBuffer;
strAllData.SetAt(nRead,0);
delete rBuffer;
delete pHttpFile;
pConnection->Close();
delete pConnection;
session.Close();
}
catch (CInternetException* e)
{
bInvalid = TRUE;
TCHAR szError[MAX_PATH];
e->GetErrorMessage(szError,MAX_PATH);
e->Delete();
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
}
if (pConnection)
{
pConnection->Close();
delete pConnection;
}
session.Close();
}
AfxMessageBox(strAllData);
CInternetSession session;
CHttpConnection* pConnection = NULL;
CHttpFile* pHttpFile = NULL;
DWORD dwStart = GetTickCount();
DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
CString strServer;
CString strObject;
INTERNET_PORT nPort;
AfxParseURL( _T("http://www.google.com/"),dwServiceType,strServer,strObject,nPort );
CString strAllData;
BOOL bInvalid = FALSE;
try
{
pConnection = session.GetHttpConnection(strServer,INTERNET_FLAG_KEEP_CONNECTION,INTERNET_INVALID_PORT_NUMBER,NULL, NULL);
pHttpFile = pConnection->OpenRequest(_T("GET"), strObject,NULL, 0, NULL, NULL,INTERNET_FLAG_KEEP_CONNECTION);
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = szProxy;
proxyinfo.lpszProxyBypass = NULL;
session.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000 * 10);
session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,1000 * 20);
pHttpFile->SendRequest(NULL);
DWORD nFileSize = pHttpFile->GetLength();
char *rBuffer = new char[nFileSize];
int nRead = pHttpFile->Read(rBuffer, nFileSize);
pHttpFile->Close();
strAllData = rBuffer;
strAllData.SetAt(nRead,0);
delete rBuffer;
delete pHttpFile;
pConnection->Close();
delete pConnection;
session.Close();
}
catch (CInternetException* e)
{
bInvalid = TRUE;
TCHAR szError[MAX_PATH];
e->GetErrorMessage(szError,MAX_PATH);
e->Delete();
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
}
if (pConnection)
{
pConnection->Close();
delete pConnection;
}
session.Close();
}
AfxMessageBox(strAllData);
解决方案 »
- 关于停靠的问题 vc
- DirectShow可以把一个视频切割成几个相同大小的视频吗?
- 想了两天,不理解,求真理……
- 动态库和静态库
- 请问 const char(btDeviceID) 与(const char)btDeviceID 有什么不同呢?
- ***如何知道别人正在登陆我的服务器?***
- 在线等候,立即给分
- 我怎样才能给一个工程里的所有文件一起改名?谢谢
- 如何获取flash中的FS_command
- 笨鸟再问:已经用IHTMLElementCollection得到元素集合,怎么得到具体的IHTMLElement
- error C2601: 'OnMouseWheel' : local function definitions are illegal
- 如何实现可以输入的下拉框!,急!请各位大哥帮帮忙阿!
1:char *rBuffer= new char[nFileSize];
2:int nRead = pHttpFile->Read(rBuffer, nFileSize);
3:strAllData = rBuffer;
4:strAllData.SetAt(nRead,0);
unicode是两个字节表示一个字符,而char是一个字节表示一个字符,
这两句话在MBCS下可以正常通过,说明对方过来的数据也是多字节的数据流,而你把多字节字符集(MBCS)直接赋值给Unicode字符串strAllData,出来的一定是乱字符。解决办法:将3及4行改为
#ifdef _UNICODE
::MultiByteToWideChar(CP_OEMCP , MB_COMPOSITE , rBuffer , nRead , strAllData.GetBuffer(nRead+1) , nRead+1);
#endif
在使用完strAllData后添加下面的代码
#ifdef _UNICODE
strAllData.ReleaseBuffer( );
#endif
#ifdef _UNICODE
::MultiByteToWideChar(CP_OEMCP , MB_COMPOSITE , rBuffer , nRead , strAllData.GetBuffer(nRead+1) , nRead+1);
#else
strAllData= rBuffer;
#endif
strAllData.SetAt(nRead,0);
CString 可以自动转换 MBCS 到Unicode ,设置断点会发现
是在这里出问题
pHttpFile->SendRequest(NULL);然后抛出异常
session.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
或者在MBCS下编译就可以正常取到数据