Well I would like to use the default Internet Explorer connection proxy settings to make a request in cURL in C++, this is my example code:
CURL *curl;
CURLcode result;
curl = curl_easy_init();
char errorBuffer[CURL_ERROR_SIZE];
if (curl)
{
// Now set up all of the curl options
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
// Attempt to retrieve the remote page
result = curl_easy_perform(curl);
// Always cleanup
curl_easy_cleanup(curl);
}
How do I do to retrieve the Proxy Internet Explorer settings and then pass to my cURL so it can be able to make requests using the proxy ?
Thanks in advance.
The WinHttpGetIEProxyConfigForCurrentUser function retrieves the Internet Explorer proxy configuration for the current user.
#include "stdafx.h"
#include <Windows.h>
#include <Winhttp.h>
#include <iostream>
using namespace std;
void main()
{
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG MyProxyConfig;
if(!WinHttpGetIEProxyConfigForCurrentUser(&MyProxyConfig))
{
//check the error DWORD Err = GetLastError();
DWORD Err = GetLastError();
cout << "WinHttpGetIEProxyConfigForCurrentUser failed with the following error number: " << Err << endl;
switch (Err)
{
case ERROR_FILE_NOT_FOUND:
cout << "The error is ERROR_FILE_NOT_FOUND" << endl;
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
cout << "ERROR_WINHTTP_INTERNAL_ERROR" << endl;
break;
case ERROR_NOT_ENOUGH_MEMORY:
cout << "ERROR_NOT_ENOUGH_MEMORY" << endl;
break;
default: cout << "Look up error in header file." << endl;
}//end switch
}//end if
else
{
//no error so check the proxy settings and free any strings
cout << "Auto Detect is: " << MyProxyConfig.fAutoDetect << endl;
if(NULL != MyProxyConfig.lpszAutoConfigUrl)
{
wcout << "AutoConfigURL (MyProxyConfig.lpszAutoConfigUrl) is: " << MyProxyConfig.lpszAutoConfigUrl << endl;
GlobalFree(MyProxyConfig.lpszAutoConfigUrl);
}
if(NULL != MyProxyConfig.lpszProxy)
{
wcout << "AutoConfigURL (MyProxyConfig.lpszProxy) is: " << MyProxyConfig.lpszProxy << endl;
GlobalFree(MyProxyConfig.lpszProxy);
}
if(NULL != MyProxyConfig.lpszProxyBypass)
{
wcout << "AutoConfigURL (is: " << MyProxyConfig.lpszProxyBypass << endl;
GlobalFree(MyProxyConfig.lpszProxyBypass);
}
}//end else
cout << "finished!";
system("PAUSE");
}//end main
You want to use InternetQueryOption
with INTERNET_OPTION_PROXY
(documentation) to find the current proxy setting and then it's just a question of passing the proxy setting to curl as normal.
first,use windows api to get the proxy server, then use the curl_easy_setopt to set the proxy to the curl.
Get the proxy:
WinHttpGetIEProxyConfigForCurrentUser
this api can get the proxy,but no WPAD. And if someone use "Use automatic proxy configuration",should use the WinHttpGetProxyForUrl
api to get the proxy.All in msdn:http://msdn.microsoft.com/en-us/library/windows/desktop/aa384096(v=vs.85).aspx
Set the proxy:curl_easy_setopt(hCurl, CURLOPT_PROXY, astrProxy.c_str());