title: WinInet how to use sock proxy
body: <p>I'd like to use WinInet to access a web page but with a sock4/sock5 proxy.</p>
  <p>Here is what I've tried:</p>
  <pre class="lang-cpp prettyprint-override"><code>HINTERNET hOpen = InternetOpenA(helper.USER_AGENT, INTERNET_OPEN_TYPE_PROXY, (&quot;SOCKS5=200.100.5.1:702&quot;, NULL, 0);
  </code></pre>
  <p>This doesn't seem to work, I'm supplying an invalid sock proxy so that when it does the web page connection it should not go through, but in my tests it's going through meaning the sock proxy is not being used but rather my actual plain internet.</p>
  <p>I've even made a helper function to set the proxy in <code>InternetSetOption()</code> as well:</p>
  <pre class="lang-cpp prettyprint-override"><code>bool changeProxy(const char* socket) {
      char strProxyList[MAX_PATH];
      bool success = false;
  
      memset(strProxyList, 0, MAX_PATH);
  
      lstrcpyA(strProxyList, &quot;SOCKS5=&quot;);
  
      INTERNET_PROXY_INFO proxy;
  
      proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
      proxy.lpszProxy = (LPCTSTR)strProxyList;
      proxy.lpszProxyBypass = NULL;
  
      if (TRUE == InternetSetOptionA(hOpen, INTERNET_OPTION_PROXY, &amp;proxy, sizeof(INTERNET_PROXY_INFO)))
      {
          printf(&quot;Socket set to %s\n&quot;, socket);
          success = true;
      }
      else
      {
          printf(&quot;Failed to set socket to %s\n&quot;, socket);
      }
  
      InternetSetOptionA(NULL, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, NULL, 0);
      return success;
  }
  </code></pre>
  <p>In which I've tried calling this function after the <code>InternetOpenA()</code> and right before the <code>InternetConnectA()</code> function, but for whatever reason it doesn't use the proxy but rather my plain internet.</p>
  <p>Here is my connection code snippet:</p>
  <pre class="lang-cpp prettyprint-override"><code>hConnect = InternetConnectA(hOpen, host, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, 3, 0, 0);
  hRequest = HttpOpenRequestA(hConnect, &quot;POST&quot;, uri, NULL, CHECKER_URL, NULL, INTERNET_FLAG_SECURE, 0);
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>Using this code, I was able to get the proxy set.</p>
  <p>But, for some reason, it only allows me to set the proxy one time and never again while the process is running:</p>
  <pre class="lang-cpp prettyprint-override"><code>HINTERNET hOpen = InternetOpenA(helper.USER_AGENT, 0, NULL, NULL, 0);
  INTERNET_PROXY_INFO proxy = { 0 };
  
  proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
  proxy.lpszProxy = (LPCTSTR)socket;
  proxy.lpszProxyBypass = NULL;
      
  if (UrlMkSetSessionOption(INTERNET_OPTION_PROXY, &amp;proxy, sizeof(proxy), 0) != 0)
  {
      success = false;
  }
  UrlMkSetSessionOption(INTERNET_OPTION_PROXY_SETTINGS_CHANGED, NULL, 0, 0);
  </code></pre>
  <p>I want to change the proxy mid-program after doing this once, but using the same code the proxy change a 2nd time doesn't take affect.</p>
  <p>I've even tried closing the handle and re-instantiating it, but no luck.</p>
  <p>When trying to use:</p>
  <pre class="lang-cpp prettyprint-override"><code>InternetSetOptionA(hOpen, INTERNET_OPTION_PER_CONNECTION_OPTION, &amp;proxy, sizeof(INTERNET_PROXY_INFO)))
  </code></pre>
  <p>I get a return value of false, so it's not setting that way, for some reason. But using <code>INTERNET_OPTION_PROXY</code> sets it.</p>