SoapHttpClientProtocol 缓存代理凭据? [英] SoapHttpClientProtocol caches proxy credentials?

查看:52
本文介绍了SoapHttpClientProtocol 缓存代理凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在 .NET4 中,SoapHttpClientProtocol 似乎缓存了代理凭据.有谁知道这是否属实,以及如何刷新此缓存?如果我的用户更改了他们的凭据,我想尝试一下,但是一旦我获得任何 SoapHttpClientProtocol 成功连接,任何调用 invoke 似乎都可以工作.

I've noticed in .NET4 that the SoapHttpClientProtocol appears to cache proxy credentials. Does anybody know if this is true, and how to refresh this cache? If my users change their credentials I would like to try them, but once I get any SoapHttpClientProtocol to successfully connect, any call to invoke appears to work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web.Services;
    class Program : System.Web.Services.Protocols.SoapHttpClientProtocol
{
    public Program()
    {
        base.Url = "something";
    }
    static void Main(string[] args)
    {
        Program x = new Program();
        x.Proxy = new WebProxy("basicproxy", 2121);
        x.Proxy.Credentials = new NetworkCredential("proxyuser", "IncorrectPassword");
        Console.WriteLine("Attempt with proxyuser and IncorrectPassword: " + x.NoOp());

        x.Proxy.Credentials = new NetworkCredential("proxyuser", "password");
        Console.WriteLine("Attempt with proxyuser and password: " + x.NoOp());

        x.Proxy.Credentials = new NetworkCredential("proxyuser", "IncorrectPassword");
        Console.WriteLine("Attempt with proxyuser and IncorrectPassword: " + x.NoOp());

        Program y = new Program();
        y.Proxy = new WebProxy("basicproxy", 2121);
        y.Proxy.Credentials = new NetworkCredential("proxyuser", "IncorrectPassword");
        Console.WriteLine("Attempt with proxyuser and IncorrectPassword: " + y.NoOp());
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("...", RequestNamespace = "...", ResponseNamespace = "...", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public bool NoOp()
    {
        try
        {
            object[] results = this.Invoke("NoOp", new object[0]);
            return ((bool)(results[0]));
        }
        catch (WebException e)
        {
            if (e.Response != null && ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.ProxyAuthenticationRequired)
            {
                Console.WriteLine("incorrect Credential attempt!");
            }
            else
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
        return false;
    }

这个程序的输出(我预计最后两个是假的)

The output of this program (I'm expecting the last two to be false)

incorrect Credential attempt!
Attempt with proxyuser and IncorrectPassword: False
Attempt with proxyuser and password: True
Attempt with proxyuser and IncorrectPassword: True
Attempt with proxyuser and IncorrectPassword: True

推荐答案

使用 Reflector 快速查看 HttpWebRequest 会发现它没有根据代理凭据.因此,它为所有这些请求重用相同的 http 连接池,并且由于相同的连接在第一次成功代理身份验证后保持活动状态,它甚至不会尝试使用为最后两个请求提供的凭据.

A quick look at HttpWebRequest with Reflector shows that it doesn't differentiate ServicePoint objects based on the proxy credentials. So it reuses the same http connection pool for all those requests and since the same connection stays alive after the first successful proxy authentication, it doesn't even try to use the credentials provided for the last two requests.

您可以尝试在设置 Proxy 属性时将 ConnectionGroupName 属性设置为包含代理用户名和密码的某个字符串(可能使用辅助方法).

You could try setting the ConnectionGroupName property to some string containing the proxy username and password at the time you set the Proxy property (with a helper method maybe).

另一种选择是为 WebProxy 构造函数使用 proxyuser:password@basicproxy:2121 url 方案.

Another option is to use the proxyuser:password@basicproxy:2121 url scheme for the WebProxy constructor.

这篇关于SoapHttpClientProtocol 缓存代理凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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