探索 WIN32。 了解如何在您的 ASP 和 VB 项目中使用同步/异步检索 HTTP 内容。






4.55/5 (7投票s)
2001 年 7 月 30 日
3分钟阅读

159021

1817
本文展示了如何使用 WinInet 函数创建一个 ATL COM 组件,如何在 ASP 程序中使用它,以及如何从 Visual Basic 客户端测试它。它还展示了如何在此组件中使用多线程支持。
引言
本文展示了如何使用 WinInet 函数创建一个 ATL COM 组件,如何在 ASP 程序中使用它,以及如何从 Visual Basic 客户端测试它。它还展示了如何在此组件中使用多线程支持。
此组件可以在 Visual Basic、Delphi、Access 或 Microsoft SQL 中以相同的方式使用。
概述
此应用程序使用 ATL、MFC、ASP、Visual Basic 和 WinInet 函数。该应用程序有一个提供一些 HTTP 函数的 ATL 项目。它将演示如何在您的 ASP 和 Visual Basic 项目中使用此组件。本文基于 Uwe Keim 的文章“Calling scripts within scripts”——非常感谢他的好主意。当然,你会问我的组件有什么新东西。让我构建此组件的原因是 HTTP 请求的可变时间延迟。
我的解决方案是异步检索,它让用户可以更频繁地检查页面是否完成。
本文还包含一个 Visual Basic 客户端,用于测试该组件的功能。
我假设读者了解或者至少对使用 ATL 进行 COM 编程有一个大致的了解。
详细说明
该组件包含两个部分:
一个标准 WinInet 检索模块。
- Retrieve 方法。
Retrieve(BSTR newVal, BSTR *parOut)
- GetPage 函数。
bool GetPage(CString sURL, CString& sBody)
Retrieve
方法只是使用 sURL
参数调用 GetPage
函数。在函数内部,有对 WinInet API 的调用
//Open one internet connection. The “INTERNET_OPEN_TYPE_PRECONFIG” //parameter tell the function to read the default proxy settings from // the registry(the proxy settings from connections in IE): HINTERNET internet=InternetOpen("Daemon", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); //Make one connection with the desired URL. On the third parameter we //use the“INTERNET_FLAG_RELOAD” //while constrains the function not to usea local cached copy. //Take a look at the documentation of this functions to see all the // combination of parameters: HINTERNET file_handle=InternetOpenUrl(internet, sURL, NULL, 0, INTERNET_FLAG_RELOAD, 0); //Read the data from the address: InternetReadFile(file_handle, pagina, 100000, &bytes_read); //Close the connection InternetCloseHandle(internet);asp 源代码 非常简单(完整的源代码在附件文件中)
url="https://codeproject.org.cn"
Set myORetrieve = CreateObject("RetrievePage.RetrievePage.1")
ret = myORetrieve.Retrieve(url)
Response.Write ret
一个异步检索模块。
- RetrieveAsync 方法。
RetrieveAsync(BSTR newVal, BSTR *parOut)
- RetrieveAsyncCompleted 方法。
RetrieveAsyncCompleted(BSTR newVal, BSTR *parOut)
- RetrieveAsyncPage 方法。
RetrieveAsyncPage(BSTR newVal, BSTR *parOut)
- MyThreadGetPage 函数。
UINT MyThreadGetPage(LPVOID pParam)
客户端必须首先使用带有所需 URL 参数的 RetrieveAsync
方法。此方法启动一个子线程,该线程将在单独的线程中检索页面。 这样,程序将转到下一条指令,而不会等待方法完成请求。
程序必须实现一个计时器(或在用户的操作中),该计时器以一定的间隔验证 RetrieveAsyncCompleted
方法是否返回 true(已检索到请求的页面)。 只有这样,RetrieveAsyncPage
方法才会提供请求的页面。
在 RetrieveAsyncCompleted
方法返回 false 时,您可以向控制台发送一条消息,例如“请稍候。 正在加载数据……”
//Make a new thread. The “MyThreadGetPage” is the name of the //function who will resolve the HTTP request. Inside the // MyThreadGetPage function, there is one call to the previous //discussed GetPage function.: AfxBeginThread( MyThreadGetPage , this, THREAD_PRIORITY_NORMAL ) // The program use a coleection to keep all request to the component. // Add a new URL request: pObject->m_MapStringToHTTPpage[sKey] = oHTTPpage; // Look at collection to see if it is a new URL or not: m_MapStringToHTTPpage.Lookup(sURL, oHTTPpage)Visual Basic 源代码 非常简单(完整的源代码在附件文件中,位于 RetrieveVBClient 目录中)
'Launch the thread with the specified address
str = myORetrieve.RetrieveAsync(URL.Text)
'Look if the retrieved page is completed:
lRetrieveAsyncCompleted = myORetrieve.RetrieveAsyncCompleted(URL.Text)
'If it is true, read the data from the specified URL address
str = myORetrieve.RetrieveAsyncPage(URL.Text)
如果您想了解如何构建 ATL 组件,请查看 构建 ATL Crypt 组件 文章。
安装
- 将 DLL 复制到具有系统执行权限的目录中,并使用 regsvr32 命令注册它,或者将其放在 MTS 上。 将其放在 MTS 上更好,因为如果我们想修改组件并重新注册它,这是可能的,而无需重新启动计算机——在使用“阻止”您的 dll 文件的 regsvr32 命令时。
- 直接使用 visual basic 对话框控制台。 在 URL 编辑框中输入一些字符串,然后单击按钮!
- 将带有 asp 页面的目录复制到 Web 服务器上 - 然后尝试一下!