繁体   English   中英

调用Web服务时出现HTTP 407代理身份验证错误

[英]HTTP 407 proxy authentication error when calling a web service

我正在开发一个通过互联网调用第三方Web服务的.NET应用程序。 服务不使用SOAP,因此我们手动构造XML请求文档,通过HTTP将其发送到服务,并检索XML响应。

我们的代码是在正常Windows域帐户的上下文中运行的Windows服务,并且位于配置为需要NTLM身份验证的代理服务器(Microsoft ISA Server)后面。 运行我们服务的帐户有权通过代理服务器访问互联网。

代码如下所示:

// Create the request object.
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";

// Configure for authenticating proxy server requiring Windows domain credentials.
request.Proxy = New WebProxy(proxyAddress) { UseDefaultCredentials = true };

// Set other required headers.
request.Accept = acceptableMimeType;
request.Headers.Add(HttpRequestHeader.AcceptCharset, acceptableCharset);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "none");
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-gb");
request.Headers.Add(HttpRequestHeader.CacheControl, "no-store");
request.Headers.Add(HttpRequestHeader.ContentEncoding, "none");
request.Headers.Add(HttpRequestHeader.ContentLanguage, "en-gb");
request.ContentType = requestMimeType;
request.ContentLength = requestBytes.Length;

// Make the method call.
using(Stream stream = request.GetRequestStream()) {
    stream.Write(requestBytes, 0, requestBytes.Length);
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

// Extract the data from the response without relying on the HTTP Content-Length header
// (we cannot trust all providers to set it correctly).
const int bufferSize = 1024 * 64;
List<byte> responseBytes = new List<byte>();
using(Stream stream = new BufferedStream(response.GetResponseStream(), bufferSize)) {
    int value;
    while((value = stream.ReadByte()) != -1) {
        responseBytes.Add((byte) value);
    }
}

如果代理服务器已关闭,或者URL已列入白名单而不需要身份验证,则此方法可以正常工作,但只要身份验证处于活动状态,它就会因HTTP 407错误而失败。

我将上面的代码放在测试工具中,并尝试了我能想到的每个方法来配置request.Proxy属性,但没有成功。

然后我注意到我们要调用的所有第三方Web服务都是HTTPS。 当我尝试将它们作为HTTP访问时,代理身份验证开始工作。 是否有一些额外的箍我必须跳过才能获得代理身份验证和HTTPS才能很好地发挥作用?

PS:开源SmoothWall代理服务器出现同样的问题,所以我不能把它作为ISA Server中的错误写出来。

PPS:我知道您可以在app.config配置代理设置,但是(a)在代码中执行它不应该有任何区别,并且(b)应用程序设计要求我们从数据库中读取代理设置运行。

您是否尝试在app.config中设置代理?

要禁用代理,请在App.config文件中添加以下配置

<system.net>
  <defaultProxy enabled="false" useDefaultCredentials="false">
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>

要启用代理并使用默认代理设置(在IE中指定),请在App.config中添加此配置

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

我确实有类似的情况

您是否注意到在运行代码之前访问互联网时它是否有效? 如果你没有访问互联网多年(我20分钟)你得到错误。

您是否尝试过直接设置代理凭据?

//setup the proxy
request.Proxy = new WebProxy("proxyIp", 8080);
request.Proxy.Credentials = CredentialCache.DefaultCredentials;

我希望这也能解决你的问题

我想我必须要把这个问题写下来。 我原来发布的代码似乎确实有效。 我们的代理服务器非常不可靠; 一分钟它将阻止任何软件的互联网连接,然后它将允许它。 IT人员对此无能为力,我们(IT部门以外的所有人)无权对网络基础架构进行更改。

如果有人对如何“强化”我的代码以补偿不可靠的代理服务器有任何想法,那么我有兴趣听到它们。 :-)

代理服务器的证书有问题吗? 如果您的服务无法建立HTTPS,则会引发错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM