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

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

问题描述

我写一个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,歌剧等?
  • 不同的例程
  • 我知道,我也许可以读出的值直接,如果他们是手动配置相应的注册表位置,但是这也与合作自动检测代理服务器?我连与该选项打扰,或者是(几乎)从来没有使用过?

在人们开始提示选择:我使用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(),这是在<一个记录href=\"http://msdn.microsoft.com/en-us/library/aa384096(VS.85).aspx\">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天全站免登陆