Windows Mobile上的CInternetSession :: OpenURL导致错误12029(无法连接)

时间:2009-05-05 16:44:37

标签: c++ http windows-mobile mfc

我正在尝试通过在Windows Mobile 5上调用CInternetSession :: OpenUrl来使用HTTP访问数据(使用MFC在C ++中编码)。我总是得到一个错误代码为12029的异常(无法连接)。

我怀疑我需要先使用Connection Manager API创建连接。有人可以证实吗?

我将根据此处的信息(http://msdn.microsoft.com/en-us/magazine/dd263096.aspx)尝试对其进行编码,如果合适,我将报告我的经验作为答案。获得其他输入也很好。

我已使用此代码成功打开了连接:

// Find out which type of connection is needed for this URL.
GUID guid;
HRESULT hresult = ConnMgrMapURL((LPCTSTR)url,&guid,NULL);
if (!SUCCEEDED(hresult))
{
delete [] url;
aError = CartoType::KErrorInternetIo;
return NULL;
}

// Get a connection.
CONNMGR_CONNECTIONINFO cinfo;
memset(&cinfo,0,sizeof(cinfo));
cinfo.cbSize = sizeof(cinfo);
cinfo.bDisabled = FALSE;
cinfo.bExclusive = FALSE;
cinfo.guidDestNet = guid;
cinfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
cinfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
cinfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
DWORD status;
hresult = ConnMgrEstablishConnectionSync(&cinfo,&iConnectionHandle,15000,&status);

我知道它有效,因为它将状态设置为CONNMGR_STATUS_CONNECTED;尽管如此,我之后立即致电CInternetSession::OpenURL并抛出异常。

1 个答案:

答案 0 :(得分:1)

这里有一些有用的代码。它使用较低级别的Windows API,而不是MFC。也许它不理想并且包含冗余(我真的需要ConnMgr调用吗?),但它确实有效:

// Find out which type of connection is needed for this URL.
GUID guid;
HRESULT hresult = ConnMgrMapURL((LPCTSTR)url,&guid,NULL);
if (!SUCCEEDED(hresult))
    {
    delete [] url;
    aError = CartoType::KErrorInternetIo;
    return NULL;
    }

// Get a connection.
CONNMGR_CONNECTIONINFO cinfo;
memset(&cinfo,0,sizeof(cinfo));
cinfo.cbSize = sizeof(cinfo);
cinfo.bDisabled = FALSE;
cinfo.bExclusive = FALSE;
cinfo.guidDestNet = guid;
cinfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
cinfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
cinfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
DWORD status;
hresult = ConnMgrEstablishConnectionSync(&cinfo,&iConnectionHandle,15000,&status);

HINTERNET hinternet = InternetOpen(_T("CartoType"),INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
HINTERNET hfile = InternetOpenUrl(hinternet,(LPCTSTR)url,NULL,0,0,1);

这将返回一个有效的句柄,我可以使用InternetReadFile读取,然后使用InternetCloseHandle关闭。