如何找出浏览器的代理设置? [英] How do I find out the browser's proxy settings?

查看:44
本文介绍了如何找出浏览器的代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Windows 编写一个命令行工具,它使用 libcurl 从互联网下载文件.

I am writing a command-line tool for Windows that uses libcurl to download files from the internet.

显然,当用户在代理服务器后面时,下载不起作用,因为需要配置代理.然而,我想让我的工具尽可能简单,而不必让用户不得不配置代理.我的工具甚至没有配置文件,因此用户必须在每个命令上传递代理设置,或者设置环境变量或其他类似的东西——太麻烦了.

Obviously, the downloading doesn't work when the user is behind a proxy server, because the proxy needs to be configured. I want to keep my tool as simple as possible however, and not have to burden the user with having to configure the proxy. My tool doesn't even have a config file, so the user would otherwise have to pass in the proxy settings on every command, or set an environment variable or somesuch -- way too much hassle.

所以我想,每个人的浏览器通常都已经正确设置,代理配置等等.即使是最基本的用户也是如此,否则他们的互联网将无法正常工作".

So I thought, everyone's browser will usually already be set up properly, proxy configured and everything. This will be true for even the most basic user because otherwise "their internet wouldn't work".

所以我想我可以通过查看IE的代理设置来确定是否使用代理.

So I figure that I can find out whether to use a proxy by looking at IE's proxy settings.

我该怎么做?更具体地说:

How do I go about this? More specifically:

  • 在 Windows 中是否有一组代理设置"供所有浏览器(可能是 IE)使用,或者我是否必须为 IE、Firefox、Opera 等编写不同的例程?
  • 我知道,如果手动配置,我可能可以直接从相应的注册表位置读取值,但这也适用于自动检测代理服务器"吗?我什至需要为那个选项而烦恼,还是(几乎)从未使用过它?

在人们开始建议替代方案之前:我使用的是 C,所以我只能使用 Win32 API,而且我真的很想继续使用 C 和 libcurl.

Before people start suggesting alternatives: I'm using C, so I'm limited to the Win32 API, and I really really want to keep using C and libcurl.

推荐答案

您正在寻找的函数是 WinHttpGetIEProxyConfigForCurrentUser(),该函数记录在 http://msdn.microsoft.com/en-us/library/aa384096(VS.85).aspx.默认情况下,Firefox 和 Opera 使用此函数来获取其代理设置,但您可以针对每个浏览器覆盖它们.不过不要那样做.正确的做法(其他人都这样做)是获取 IE 设置并假设它们是正确的,因为它们几乎总是正确的.

The function you're looking for is WinHttpGetIEProxyConfigForCurrentUser(), which is documented at http://msdn.microsoft.com/en-us/library/aa384096(VS.85).aspx. This function is used by Firefox and Opera to get their proxy settings by default, although you can override them per-browser. Don't do that, though. The right thing to do (which is what everybody else does) is to just get the IE settings and assume that they're correct, since they almost always are.

以下是相关逻辑的示例,您应该根据自己的需要对其进行调整:

Here's a sample of the relevant logic, which you should adapt for your needs:

if( WinHttpGetIEProxyConfigForCurrentUser( &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;

    // here we reset fAutoProxy in case an auto-proxy isn't actually
    // configured for this url
    fAutoProxy = WinHttpGetProxyForUrl( hiOpen, pwszUrl, &autoProxyOptions, &autoProxyInfo );
}

if ( fAutoProxy )
{
    // set proxy options for libcurl based on autoProxyInfo
}
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"
    }
    else
    {
        // there is no auto proxy and no manually configured proxy
    }
}

这篇关于如何找出浏览器的代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆