c# - 使用网络服务时需要代理身份验证错误

标签 c# wcf web-services proxy web

我有一个在 Windows 7 和 Windows XP 上运行的 .Net 4.0 Windows 应用程序。 应用程序中的一个模块使用他们的 Web 服务连接到互联网上的一个 URL [例如 http://abc.com/xyz/MyWebService]。此功能一直有效,直到上周我开始收到此错误消息时在网络服务上调用方法

There was no endpoint listening at http://abc.com/xyz/MyWebService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.And the InnerException was:HTTP Error 407 Proxy authentication required

我 [在 Windows 7 上] 多次重新运行此代码,我发现此行为是随机的...即有时我能够在服务器上调用 webservice 方法而不会出现任何错误。

不确定幕后发生了什么以及可以解释这种随机行为的原因。 此外,此错误不会出现在装有 Windows XP 且位于公司内部网上不同地理位置的计算机上。

有什么想法吗?

注意:当我在 app.config 中添加以下节点时,错误似乎消失了:

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
    </defaultProxy>
</system.net>

最佳答案

在连接我们网络服务的客户端之一,我因这个问题经历了将近 2 周的痛苦。

您需要使用实现 IWebProxy 的自定义代理模块覆盖 System.Net 配置

第 1 步:创建程序集 (DLL) 第二步:在其中添加如下类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;

namespace MyProjectNameSpace.Utils.WebProxy
{
    public class CustomWebProxy : IWebProxy
    {
        public ICredentials Credentials
        {
            get
            {
                string _proxyUserName  = ConfigurationManager.AppSettings["ProxyUserName" ] as string ?? "";
                string _proxyPassword  = ConfigurationManager.AppSettings["ProxyPassword" ] as string ?? "";
                string _useProxyDomain = ConfigurationManager.AppSettings["UseProxyDomain"] as string ?? "";
                string _proxyDomain    = ConfigurationManager.AppSettings["ProxyDomain"   ] as string ?? "";

                return String.IsNullOrEmpty(_proxyDomain)
                    ? new NetworkCredential(_proxyUserName, _proxyPassword)
                    : new NetworkCredential(_proxyUserName, _proxyPassword, _proxyDomain);
            }
            set { }
        }

        public Uri GetProxy(Uri destination)
        {
            string _proxyServer = ConfigurationManager.AppSettings["ProxyServer"] as string ?? "";
            Uri result = new Uri(_proxyServer);
            return result;
        }

        public bool IsBypassed(Uri host)
        {
            return false;
        }
    }
}

第三步:编译到 Release模式 第 4 步:将 DLL 引用到您的 WCF 客户端项目 第 5 步:打开 WCF 客户端项目的 Web.Config 或 App.Config 文件并添加以下配置。

<appSettings>
        <add key="ProxyServer" value="http://192.168.1.254:9099"/>
        <add key="ProxyUserName" value="dipak.r"/>
        <add key="ProxyPassword" value="password"/>
        <add key="UseProxyDomain" value="true"/>
        <add key="ProxyDomain" value="DOMAINNAME"/>
</appSettings>

添加或更改以下部分。

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="false">
      <module type="MyProjectNameSpace.Utils.WebProxy.CustomWebProxy, MyProjectNameSpace.Utils.WebProxy"/>
    </defaultProxy>
</system.net>

关于c# - 使用网络服务时需要代理身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176269/

相关文章:

c# - 只读 Dictionary<int, List<int>>

c# - MySQL 时区支持

wcf - 部署asp.net mvc2时出现IIS错误

java - 如何让 CXF 为 WSDL 生成外部 XSD?

java - 如何在 Spring Boot 中禁用 ErrorPageFilter?

c# - 从 HandleErrorAttribute 中获取 Action 的属性

c# - LoginAsync 缺失

c# - 通过 WCF 服务调用 IEnumerable 时,我收到 The underlying connection was closed 错误

c# - 为什么 Unity.Wcf 中的服务主机无法正确解析服务实例?

http - 使用具有相同值的重复响应 header 可以吗?