首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SoapHttpClientProtocol缓存代理凭据?

SoapHttpClientProtocol缓存代理凭据?
EN

Stack Overflow用户
提问于 2010-11-15 08:25:57
回答 1查看 2.7K关注 0票数 1

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

代码语言:javascript
复制
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;
    }

这个程序的输出(我希望最后两个是false)

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-15 10:59:24

使用Reflector快速查看一下HttpWebRequest,就会发现它不会根据代理凭据来区分ServicePoint对象。因此,它为所有这些请求重用相同的http连接池,并且由于相同的连接在第一次成功的代理身份验证后仍然有效,因此它甚至不会尝试使用为最后两个请求提供的凭据。

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4180572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档