我在.net4中注意到SoaphttpClient Protocol似乎可以缓存代理凭据。有人知道这是否是真的,以及如何刷新此缓存?如果我的用户更改他们的凭据,我想尝试一下,但是一旦我获得了任何SoaphttpClient Protocolocol即可成功连接,请使用任何调用的调用。

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;
    }

该程序的输出(我希望最后两个是错误的)

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
有帮助吗?

解决方案

快速看 HttpWebRequest 用反射器显示它没有区别 ServicePoint 基于代理凭据的对象。因此,它可以为所有这些请求重用相同的HTTP连接池,并且由于在第一个成功的代理身份验证之后相同的连接保持活力,因此甚至没有尝试使用为最后两个请求提供的凭据。

您可以尝试设置 ConnectionGroupName 属性到某些字符串,包含代理用户名和密码,当时您设置 Proxy 属性(也许使用辅助方法)。

另一个选项是使用proxyuser:password@basicproxy:2121 URL方案 WebProxy 构造函数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top