简体   繁体   中英

How to get the system proxy settings in Linux using c

如何使用C或C ++在Linux中读取系统代理设置值

您正在寻找此功能getenv ("http_proxy")

System proxy settings are generally stored in environment variables like HTTP_PROXY, HTTPS_PROXY etc.

'C' allows us to read enrolment variables via adding an extra argument envp to the main() function as shown.

    int main (int argc, char *argv[], char *envp[])
    {
      char *http_proxy, *https_proxy;
      http_proxy = getenv("HTTP_PROXY");
      https_proxy = getenv("HTTPS_PROXY");
      printf ("Proxy settings :: %s on %s.\n", http_proxy, https_proxy);
      return 0;
    }

This should do the trick depending on what variables you would want to process.

Most Linux distributions that I've seen do not have the notion of a "system proxy". The desktop environments that run on top of Linux (KDE, Gnome, etc....) generally have configuration options to set up a proxy, which most applications written for that desktop will then have access to, but how to look that up in code will be different depending on which environment you're running. Also, running eg KDE apps under Gnome or vice versa may not get the same results, unless both have been configured properly. Because of this and a number of other things, many individual applications have their own way to set proxies. One of those possible ways, that works for some applications is the environment variables mentioned in other answers (other possibilities being various configuration files, or connecting to one of the configuration services like gconf). If you're writing a new app and just want to be able to set and use a proxy in that app, this approach is probably one of the simplest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM