Use Wininet SetOption to set proxy

Source: Internet
Author: User

This is an example of downloading network files using a proxy in msdn.

Cstring gehttptfile (const char * URL)
{
Cstring szcontent;
Char strproxylist [max_path], strusername [64], strpassword [64];
// In this case "proxya" is the proxy server name, and "8080" is its port
Strcpy (strproxylist, "proxya: 8080 ");
Strcpy (strusername, "myusername ");
Strcpy (strpassword, "mypassword ");
DWORD dwservicetype = afx_inet_service_http;
Cstring szserver, szobject;
Internet_port nport;
AfxParseURL (url, dwServiceType, szServer, szObject, nPort );
CInternetSession mysession;
CHttpConnection * pConnection;
CHttpFile * pHttpFile;
PConnection = mysession. GetHttpConnection (szServer,
INTERNET_FLAG_KEEP_CONNECTION,
INTERNET_INVALID_PORT_NUMBER,
NULL, NULL );
PHttpFile = pConnection-> OpenRequest ("GET", szObject,
NULL, 0, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION );
// Here for proxy
INTERNET_PROXY_INFO proxyinfo;
Proxyinfo. dwAccessType = INTERNET_OPEN_TYPE_PROXY;
Proxyinfo. lpszProxy = strProxyList;
Proxyinfo. lpszProxyBypass = NULL;
Mysession. SetOption (INTERNET_OPTION_PROXY, (LPVOID) & proxyinfo, sizeof (INTERNET_PROXY_INFO ));
PHttpFile-> SetOption (INTERNET_OPTION_PROXY_USERNAME, strUsername, strlen (strUsername) + 1 );
PHttpFile-> SetOption (INTERNET_OPTION_PROXY_PASSWORD, strPassword, strlen (strPassword) + 1 );

PHttpFile-> SendRequest (NULL );
DWORD nFileSize = pHttpFile-> GetLength ();
LPSTR rbuf = szContent. GetBuffer (nFileSize );
Uint ubytesread = phttpfile-> Read (rbuf, nfilesize );
Szcontent. releasebuffer ();
Phttpfile-> close ();
Delete phttpfile;
Pconnection-> close ();
Delete pconnection;
Mysession. Close ();
Return szcontent;
}

Assert (m_astrproxies.getsize ()> 0 );
Cstring strproxy = m_astrproxies [m_iproxyindex];
If (strproxy. isempty ())
{
Internet_proxy_info IPI;
IPI. dwaccesstype = internet_open_type_direct;
IPI. lpszproxy = NULL;
IPI. lpszproxybypass = NULL;
Urlmksetsessionoption (internet_option_proxy, & IPI, sizeof (IPI), 0 );
}
Else
{
Internet_proxy_info IPI;
IPI. dwaccesstype = internet_open_type_proxy;
IPI. lpszproxy = strproxy;
IPI. lpszproxybypass = NULL;
Urlmksetsessionoption (internet_option_proxy, & IPI, sizeof (IPI), 0 );
}

Setoption (internet_option_proxy_username, (lpvoid) szid,: lstrlen (szid ));
Setoption (internet_option_proxy_password, (lpvoid) szpassword,: lstrlen (szpassword ));

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.