My God, I met a windows api BUG! And contributed an http file download VC code that fully supports the Proxy Server

Source: Internet
Author: User

Okay, I have encountered a windows api bug...

 

In the beginning, the program uses CInternetSession to open a Session, and then uses OpenUrl to open a CHttpFile file. This program has been working very well and can be downloaded as long as ie can access the Internet. If a proxy server is used, you only need to set it in ie. If the proxy server requires a password, you only need to first access the page in ie, enter the password, and choose to save the password. This program can also be connected through the proxy normally.

Until one day, it was installed on an ie6 windows xp Machine and cannot work.

Because the CInternetSession: OpenUrl method calls the InternetOperUrl api function, while the InternetOperUrl function has a BUG.

InternetOperUrl in IE6 environment, unless the proxy server's user name and password are consistent with the current user's user name and password, it cannot connect to the http file through the proxy server.

Why do I know this is a BUG? After ie8 is installed, InternetOperUrl will work normally-you only need to select "Save my password" when entering the proxy server password when ie accesses the Internet through proxy ", internetOperUrl can be connected normally.

In ie6, InternetOperUrl always uses the user name and password used to log on to the computer to verify with the proxy server. This is obviously an error. They fixed it later.

 

This problem caused me to go through three days. I installed n different versions of windows and ie environments and tested various programs. I was almost prepared to use my msdn technical support, finally confirm the cause of the problem.

It's easy to understand the cause. You only need to bypass the issue of mfc and use several underlying APIs to work properly. The following is the code that works normally. Three different proxy server settings are supported: 0 is set with ie (including the Password Saved by ie). 1. Do not use a proxy, be sure to connect directly. 2. Use the proxy specified by this program.

IProxyMode, bProxyNeedPassword, proxyinfo, sProxyUserName, and sProxyPassword are global variables. You can prepare these variables before calling this function.

BOOL GetHttpFile (maid, maid)

{
BOOL B;
BOOL bOK = TRUE;
DWORD dwServiceType;
CString strServer;
CString strObject;
INTERNET_PORT nPort;

BOOL bParsed = AfxParseURL (psUrl, dwServiceType, strServer, strObject, nPort );
If (! BParsed)
{
SErrMg = "the remote file address format is incorrect! ";
Return FALSE;
}

HINTERNET m_hInternet = InternetOpen (
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1 )",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL, 0 );

If (FALSE = m_hInternet)
{
InternetCloseHandle (m_hInternet );
SErrMg. Format ("failed to establish network session % s", psUrl );
Return FALSE;
}

If (iProxyMode! = 0)
{
B = InternetSetOption (NULL, INTERNET_OPTION_PROXY, (LPVOID) & proxyinfo, sizeof (proxyinfo ));
}

HINTERNET m_hConnection = InternetConnect (
M_hInternet,
StrServer,
NPort,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
INTERNET_FLAG_NO_UI,
NULL );

If (FALSE = m_hConnection)
{
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );
SErrMg. Format ("failed to establish network connection % s", psUrl );
Return FALSE;
}

If (iProxyMode = 2 & bProxyNeedPassword)
{
DWORD dwUserNameLen = sProxyUserName. GetLength () + 1;
DWORD dwUserPassLen = sProxyPassword. GetLength () + 1;

B = InternetSetOption (m_hConnection, INTERNET_OPTION_PROXY_USERNAME, (LPVOID) (LPCTSTR) sProxyUserName, dwUserNameLen );
B = InternetSetOption (m_hConnection, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID) (LPCTSTR) sProxyPassword, dwUserPassLen );
}

Static LPCTSTR s_szAcceptTypes [] = {_ T ("*/*"), NULL };

HINTERNET m_hRequest = HttpOpenRequest (
M_hConnection, _ T ("GET "),
StrObject,
_ T ("HTTP/1.0"), NULL,
S_szAcceptTypes,
INTERNET_FLAG_NO_UI | INTERNET_FLAG_KEEP_CONNECTION, // | (m_url.GetScheme () = ATL_URL_SCHEME_HTTPS )? INTERNET_FLAG_SECURE: 0)
NULL );
If (FALSE = m_hRequest)
{
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );
SErrMg. Format ("failed to open network connection % s", psUrl );
Return FALSE;
}

CString strHeaders;
// StrHeaders. Append (_ T ("Content-Type: text/xml; charset = UTF-8 \ r \ n "));

B = HttpSendRequest (m_hRequest, strHeaders, (DWORD) strHeaders. GetLength (),
NULL, 0 );

If (FALSE = B)
{
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );
SErrMg. Format ("failed to send request % s to server", psUrl );
Return FALSE;
}

Int iStatus = GetInternetRequestStatusCode (m_hRequest );

If (! (IStatus> = 200 & iStatus <300 ))
{
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );
SErrMg. Format ("An error occurred while opening the remote file. Error code: % d", iStatus );
Return FALSE;
}

Byte pData [65535];
DWORD dwReadedLen;
DWORD dwWrittenLen;

HANDLE hfile = CreateFile (psLocalFile, GENERIC_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
If (hfile = INVALID_HANDLE_VALUE)
{
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );

SErrMg. Format ("failed to create local file % s", psLocalFile );

Return FALSE;
}

While (1)
{
B = InternetReadFile (m_hRequest, (LPVOID) pData, sizeof (pData), & dwReadedLen );
If (B = FALSE)
{
CloseHandle (hfile );
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );

SErrMg. Format ("failed to read file % s", psUrl );
Return FALSE;
}
If (dwReadedLen = 0)
{
Break;
}
B = WriteFile (hfile, pData, dwReadedLen, & dwWrittenLen, NULL );
If (B = FALSE)
{
CloseHandle (hfile );
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );
SErrMg. Format ("failed to write local file % s", psLocalFile );
Return FALSE;
}

}

CloseHandle (hfile );
InternetCloseHandle (m_hRequest );
InternetCloseHandle (m_hConnection );
InternetCloseHandle (m_hInternet );
SErrMg. Format ("Download successful ");
Return TRUE;

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.