Curl supports HTTP proxy, socket4 proxy, and socket5 proxy, but ie proxy is the most common in windows. That is, set the proxy in IE browser.
Because curl does not have direct support options, if you want to Implement IE proxy in curl, you need to do some small work.
The idea is as follows: Obtain the proxy type, proxy IP address, and proxy port information in IE browser through the API provided by windows. Set proxy through the function provided by URL.
I. First, let's get to know three functions.
1. winhttpgtieproxyconfigforcurrentuser Function
Bool winapi winhttpgtieproxyconfigforcurrentuser (_ inout _ winhttp_current_user_ie_proxy_config * pproxyconfig );
TypedefStruct{Bool fautodetect; -- corresponds to "automatic persistence setting" lpwstr lpszautoconfigurl; -- the address text box content lpwstr lpszproxy when "automatic script setting" is selected; -- corresponds to the IP address in the "Proxy Server" and the port lpwstr lpszproxybypass; -- when "the server that skips the local address" is selected, click the "advanced" button and the "exception" text box for "Proxy Settings" appears} winhttp_current_user_ie_proxy_config;
This function is used to obtain the proxy settings of the current user. According to the return value of pproxyconfig and the comments to each member in this structure, the meaning of this function should be clear.
2,Winhttpopen Function
Hinternet winapi winhttpopen (_ in_opt _ lpcwstr pwszuseragent, _ in _ DWORD dwaccesstype, _ in _ lpcwstr pwszproxyname, _ in _ lpcwstr pwszproxybypass, _ in _ DWORD dwflags );
We use this function only to obtain a WinHTTP session. Therefore, we do not need to set any parameters here, as shown in figure
Hinternet session =: winhttpopen (0, // No agent string
Winhttp_access_type_default_proxy,
Winhttp_no_proxy_name,
Winhttp_no_proxy_bypass,
Winhttp_flag_async );
3,Winhttpgetproxyforurl
Bool winapi winhttpgetproxyforurl (_ in _ hinternet hsession, -- is a handle _ in _ lpcwstr lpcwszurl returned from the winhttpopen function, -- indicates the URL address to be accessed _ in _ winhttp_autoproxy_options * pautoxyoptions, -- Some Options _ out _ winhttp_proxy_info * pproxyinfo -- return value when the corresponding proxy is searched );
TypedefStruct{DWORD dwflags; DWORD dwautodetectflags; lpcwstr lpszautoconfigurl; -- lpvoid lpvreserved in the address text box when automatic script setting is selected; DWORD dwreserved; bool identifier;} identifier;
Char * W2C (char * pcstr, size_t Len, const wchar_t * pwstr)
{
# Ifdef Win32
Int nlength = wcslen (pwstr );
Int nbytes = widechartomultibyte (cp_utf8, // specify the code page used to perform the conversion
0, // no special flags to handle unmapped characters
Pwstr, // wide character string to convert
Nlength, // The number of wide characters in that string
Null, // No output buffer given, we just want to know how long it needs to be
0,
Null, // no replacement character given
Null); // we don't want to know if a character didn't make it through the translation
// Make sure the buffer is big enough for this, making it larger if necessary
If (nbytes> Len) nbytes = Len;
Widechartomultibyte (cp_utf8, // specify the code page used to perform the conversion
0, // no special flags to handle unmapped characters
Pwstr, // wide character string to convert
Nlength, // The number of wide characters in that string
Pcstr, // put the output ASCII characters at the end of the buffer
Nbytes, // There is at least this much space there
Null, // no replacement character given
Null );
Pcstr [nbytes] = 0;
# Else
Sprintf (pcstr, "% s", pwstr );
# Endif
Return pcstr;
}
Typedef Enum _ mx_cs_proxy_type {
Mx_cs_proxy_type_noproxy = 0,
Mx_cs_proxy_type_http,
Mx_cs_proxy_type_socks4,
Mx_cs_proxy_type_socks4a,
Mx_cs_proxy_type_socks5,
# Ifdef Win32
Mx_cs_proxy_type_useie,
# Endif
} Mx_cs_proxy_type;
Bool getproxyaddr (const string & straddr, char * strdestaddr, const char * type)
{
Int nstart = straddr. Find (type );
If (nstart! =-1)
{
Nstart + = strlen (type );
Int nlen = straddr. Find (';', nstart + 1)-nstart;
Strcpy (strdestaddr, straddr. substr (nstart, nlen). c_str ());
Return true;
}
Return false;
}
Int getieproxy (const wchar_t * Host, Int & proxytype, Int & Port, char * straddr, bool buserhttps)
{
Bool fautoproxy = false;
Winhttp_proxy_info autoproxyinfo = {0 };
Winhttp_autoproxy_options autoproxyoptions = {0 };
Winhttp_current_user_ie_proxy_config ieproxyconfig = {0 };
If (winhttpgtieproxyconfigforcurrentuser (& ieproxyconfig ))
{
If (ieproxyconfig. fautodetect)
{
Fautoproxy = true;
}
If (ieproxyconfig. lpszautoconfigurl! = NULL)
{
Fautoproxy = true;
Autoproxyoptions. lpszautoconfigurl = ieproxyconfig. lpszautoconfigurl;
}
}
Else
{
// Use autoproxy
Fautoproxy = true;
}
If (fautoproxy)
{
If (autoproxyoptions. lpszautoconfigurl! = NULL)
{
Autoproxyoptions. dwflags = winhttp_autoproxy_config_url;
}
Else
{
Autoproxyoptions. dwflags = winhttp_autoproxy_auto_detect;
Autoproxyoptions. dwautodetectflags = winhttp_auto_detect_type_dhcp | winhttp_auto_detect_type_dns_a;
}
// Basic flags you almost always want
Autoproxyoptions. fautologonifchallenged = true;
Hinternet session =: winhttpopen (0, // No agent string
Winhttp_access_type_default_proxy,
Winhttp_no_proxy_name,
Winhttp_no_proxy_bypass,
Winhttp_flag_async );
// Here we reset fautoproxy In case an auto-proxy isn't actually
// Configured for this URL
Fautoproxy = winhttpgetproxyforurl (Session, host, & autoproxyoptions, & autoproxyinfo );
If (Session) winhttpclosehandle (session );
}
If (fautoproxy)
{
// Set proxy options for libcurl Based on autoproxyinfo
// Autoproxyinfo. lpszproxy
// Curl_easy_setopt (curl, curlopt_proxy, autoproxyinfo. lpszproxy );
If (autoproxyinfo. lpszproxy)
{
W2C (straddr, 256, autoproxyinfo. lpszproxy );
Proxytype = mx_cs_proxy_type_http;
Port = 0;
}
Else
{
Return-1;
}
}
Else
{
If (ieproxyconfig. lpszproxy! = NULL)
{
// IE has an explicit proxy. Set proxy options for libcurl here
// Based on ieproxyconfig
//
// Note that sometimes ie gives just a single or double colon
// For proxy or bypass list, which means "No proxy"
W2C (straddr, 256, ieproxyconfig. lpszproxy );
Proxytype = mx_cs_proxy_type_http;
Port = 0;
// may be like this: "HTTP = 127.0.0.1: 8888; HTTPS = 127.0.0.1: 8888; FTP = 127.0.0.1: 8888; socks = 127.0.0.1: 8888" "127.0.0.1: 8888 "
string strproxyaddr (straddr);
If (strproxyaddr. find ('= ')! =-1)
{< br> bool bfind = false;
If (buserhttps & getproxyaddr (strproxyaddr, straddr, "HTTPS =") bfind = true;
If (bfind = false & getproxyaddr (strproxyaddr, straddr, "HTTP =") bfind = true;
If (bfind = false & getproxyaddr (strproxyaddr, straddr, "socks =")
{< br> proxytype = mx_cs_proxy_type_socks5;
}< BR >}< br> else
{< br> proxytype = mx_cs_proxy_type_noproxy;
// There is no auto proxy and no manually configured proxy
}< BR >}
If (autoproxyinfo. lpszproxy! = NULL) globalfree (autoproxyinfo. lpszproxy );
If (autoproxyinfo. lpszproxybypass! = NULL) globalfree (autoproxyinfo. lpszproxybypass );
// If (autoproxyoptions. lpszautoconfigurl! = NULL) globalfree (autoproxyoptions. lpszautoconfigurl );
If (ieproxyconfig. lpszautoconfigurl! = NULL) globalfree (ieproxyconfig. lpszautoconfigurl );
If (ieproxyconfig. lpszproxy! = NULL) globalfree (ieproxyconfig. lpszproxy );
If (ieproxyconfig. lpszproxybypass! = NULL) globalfree (ieproxyconfig. lpszproxybypass );
Return proxytype;
}
Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384096 (V = vs.85). aspx
Http://msdn.microsoft.com/en-us/library/windows/desktop/aa384098 (V = vs.85). aspx
Http://msdn.microsoft.com/en-us/library/windows/desktop/aa384097 (V = vs.85). aspx
http://www.fengfly.com/plus/view-75365-1.html