6

我有一个使用 Web 服务的客户端程序。它在许多安装中运行良好。现在我遇到了一个新客户通过代理服务器连接到 Internet 的情况,而我的程序尝试访问 Web 服务时出现“HTTP 状态 407:需要代理身份验证”错误。

我认为所有的互联网访问配置,包括代理服务器地址、端口号和身份验证都将在控制面板的互联网选项中完成,我不必担心代码甚至应用程序中的这些。 Web 服务客户端的配置。

难道我都搞错了吗?

同时我所做的是让用户有机会配置代理用户名和密码,然后在我的代码中执行以下操作:

webServiceClient.ClientCredentials.UserName.UserName = configuredUsername;
webServiceClient.ClientCredentials.UserName.Password = configuredPassword;

但我不知道这是对的。因为在我看来,上面的 ClientCredentials 是指 Web 服务绑定/安全性,而不是 Internet 代理服务器。

我想我可以在客户那里尝试一下,但我宁愿先确定我在做什么。

4

1 回答 1

6

在另一个论坛的贡献者的帮助下,我发现了如何做这件事,该论坛正在尝试各种我已经忘记的事情。所以感谢那个现在被遗忘的人。

这是最终起作用的代码(适当地伪装,但给出了正确的想法):

    BasicHttpBinding binding = new BasicHttpBinding("APISoap"); /* APISoap is the name of the binding element in the app.config */
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
    binding.UseDefaultWebProxy = false;
    binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyIpAddress, proxyPort)); 
    EndpointAddress endpoint = new EndpointAddress("http://www.examplewebservice/api.asmx");

    WebServiceClient client = new WebServiceClient(binding, endpoint);

    client.ClientCredentials.UserName.UserName = proxyUserName;
    client.ClientCredentials.UserName.Password = proxyPassword;
于 2009-11-06T12:26:24.687 回答