CInternetSession设置超时无效

本文探讨了在使用CInternetSession时如何正确设置超时时间的问题。通过分析Microsoft产品的已知BUG,给出了两种可行的解决方案:一是采用异步WinInet模式;二是创建第二个线程来处理阻塞的WinInet API。
转贴自:http://www.softwelt.com/Know/KnowDetail-2870437.html


问题:CInternetSession设置超时?
m_pSession = new CInternetSession();
m_pSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5000);


但是发现后面使用CHttpFile和CHttpConnection连接的时候,
超时时间还是很久,不是5000毫秒

请问这个问题怎么解决?

提问时间: 2005-2-18 10:39:56 回答次数: (5) 提问人: 软界网友  我来回答
回答(1)
BUG: InternetSetOption Does Not Set Timeout Values
ID: Q176420


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft ActiveX SDK, version 1.0
Internet Client SDK, versions 4.0, 4.01
Microsoft Internet Explorer (Programming) version 5

--------------------------------------------------------------------------------


SYMPTOMS
Calling InternetSetOption (or MFC CHttpFile::SetOption) with INTERNET_OPTION_SEND_TIMEOUT or INTERNET_OPTION_CONNECT_TIMEOUT does not set the specified timeout values.



RESOLUTION
To work around the problem you can use asynchronous WinInet mode, which prevents the WinInet function call from blocking while waiting for a connection. Please see the Internet Client SDK documentation for more information about using WinInet asynchronously.

Another solution may be to create a second thread that would call blocking WinInet API. Closing the handle from within the original thread will cancel blocking API in the second thread. Please see documentation for InternetCloseHandle for more details.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.



MORE INFORMATION
InternetSetOption works for INTERNET_OPTION_RECEIVE_TIMEOUT. The receive timeout option controls how long to wait for incoming data to become available. It does not control how long to wait while connecting to or sending data to the server. If the network is down or the server is not available, a WinInet function call that makes a connection (HttpSendRequest, for example) can potentially block for a long time.

NOTES:

INTERNET_OPTION_RECEIVE_TIMEOUT no longer works in Internet Explorer 5.0. For more information, see the following article in the Microsoft Knowledge Base:


Q224318 HOWTO: Control Connection Timeout Value by Creating Second Thread
The latest version of the Wininet.dll file that shipped with Internet Explorer 5.01 (available for download) fixes all timeout problems for HTTP APIs only. FTP timeouts still cannot be changed.


© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Leon Braginski, Microsoft Corporation


Additional query words:

Keywords : kbSDKInet400 kbSDKInet401 kbIE500bug
Version : WIND
`CInternetSession` 是 MFC(Microsoft Foundation Classes)提供的一个类,用于管理 Internet 会话。它是 Windows 平台下进行 Internet 编程的一部分,可用于实现 HTTP 请求,同时也支持其他 Internet 协议,如 FTP、Gopher 等。 #### 实现 HTTP 请求的基本原理 `CInternetSession` 类封装了 Windows 的 WinInet API,提供了更高级、更易用的接口。通过创建 `CInternetSession` 对象,可以初始化一个 Internet 会话,然后使用该会话对象打开一个 HTTP 连接,发送请求并接收响应。 #### 使用示例 以下是使用 `CInternetSession` 实现 HTTP GET 请求的示例代码: ```cpp #include <afxinet.h> #include <iostream> #include <string> int main() { CInternetSession session("HttpClient"); CHttpFile* pfile = nullptr; std::string strURL = "http://www.example.com"; try { pfile = (CHttpFile*)session.OpenURL(strURL.c_str()); DWORD dwStatusCode; pfile->QueryInfoStatusCode(dwStatusCode); if (dwStatusCode == HTTP_STATUS_OK) { CString content; CString data; while (pfile->ReadString(data)) { content += data + "\r\n"; } content.TrimRight(); std::cout << (LPCTSTR)content << std::endl; } } catch (CInternetException* m_pException) { DWORD dwError = m_pException->m_dwError; m_pException->Delete(); std::cerr << "Internet exception: " << dwError << std::endl; } if (pfile) { pfile->Close(); delete pfile; } session.Close(); return 0; } ``` 上述代码中,首先创建了一个 `CInternetSession` 对象,然后使用 `OpenURL` 方法打开指定的 URL,获取一个 `CHttpFile` 对象。接着,通过 `QueryInfoStatusCode` 方法检查响应状态码是否为 200(HTTP_STATUS_OK),如果是,则读取响应内容并输出。最后,关闭文件和会话。 #### 实现 HTTP POST 请求示例 ```cpp #include <afxinet.h> #include <iostream> #include <string> int main() { CInternetSession session("HttpClient"); CHttpConnection* pConnection = nullptr; CHttpFile* pFile = nullptr; try { pConnection = session.GetHttpConnection("www.example.com", INTERNET_DEFAULT_HTTP_PORT); pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, "/path/to/post"); CString postData = "param1=value1&param2=value2"; pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded"); pFile->SendRequest(nullptr, 0, (LPVOID)(LPCTSTR)postData, postData.GetLength()); DWORD dwStatusCode; pFile->QueryInfoStatusCode(dwStatusCode); if (dwStatusCode == HTTP_STATUS_OK) { CString content; CString data; while (pFile->ReadString(data)) { content += data + "\r\n"; } content.TrimRight(); std::cout << (LPCTSTR)content << std::endl; } } catch (CInternetException* pEx) { DWORD dwError = pEx->m_dwError; pEx->Delete(); std::cerr << "Internet exception: " << dwError << std::endl; } if (pFile) { pFile->Close(); delete pFile; } if (pConnection) { pConnection->Close(); delete pConnection; } session.Close(); return 0; } ``` 在这个示例中,首先使用 `GetHttpConnection` 方法建立与服务器的连接,然后使用 `OpenRequest` 方法创建一个 POST 请求。接着,设置请求头和请求数据,并使用 `SendRequest` 方法发送请求。最后,处理响应并关闭连接和会话。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值