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