WinAPI: InternetCloseHandle function closes the handle but not the connection

前端 未结 2 612
天涯浪人
天涯浪人 2021-01-06 20:31

I call wininet\\InternetOpenUrlA, then wininet\\InternetReadFile and when I\'m done I call wininet\\InternetCloseHandle which returns True. That means that the handle succes

相关标签:
2条回答
  • 2021-01-06 21:19

    WinInet can cache and reuse connections for future requests to the same server.

    0 讨论(0)
  • 2021-01-06 21:20

    WinInet tries to re-use sockets where it can, so even when you release the handle it can choose to keep the socket active, ready for the next call to InternetOpen. Most of the time this is a good thing and you don't need to worry about it.

    If you really need it to be closed immediately, you can fool WinInet into doing this by calling InternetSetOption after your final InternetCloseHandle:

    ...
    InternetCloseHandle(hInternet);
    InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
    

    Doing this tells WinInet that global WinInet settings have changed (e.g. in the Registry) so it has no choice but to close all sockets and reset itself. However this obviously is not the intended usage and will have some performance impact if you are making lots of connections with WinInet.

    0 讨论(0)
提交回复
热议问题