【问题标题】:Setting the Internet Explorer Proxy in cURL Library Requests in C++ - Windows在 C++ 中的 cURL 库请求中设置 Internet Explorer 代理 - Windows
【发布时间】:2012-05-12 23:52:11
【问题描述】:

我想使用默认的 Internet Explorer 连接代理设置在 C++ 中的 cURL 中发出请求,这是我的示例代码:

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);
}

如何检索代理 Internet Explorer 设置,然后传递给我的 cURL,以便它能够使用代理发出请求?

提前致谢。

【问题讨论】:

    标签: c++ windows curl libcurl


    【解决方案1】:

    您想使用InternetQueryOptionINTERNET_OPTION_PROXY (documentation) 来查找当前的代理设置,然后这只是passing the proxy setting to curl 的问题。

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        【解决方案3】:

        首先使用windows api获取代理服务器,然后使用curl_easy_setopt设置代理为curl。

        获取代理: WinHttpGetIEProxyConfigForCurrentUser这个api可以获取代理,但是没有WPAD。如果有人使用“使用自动代理配置”,应该使用WinHttpGetProxyForUrl api获取代理。全部在msdn:http://msdn.microsoft.com/en-us/library/windows/desktop/aa384096(v=vs.85).aspx

        设置代理:curl_easy_setopt(hCurl, CURLOPT_PROXY, astrProxy.c_str());

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-12
          • 2015-03-22
          • 2013-01-09
          相关资源
          最近更新 更多