调用Web服务时,HTTP 407代理身份验证错误 [英] HTTP 407 proxy authentication error when calling a web service

查看:487
本文介绍了调用Web服务时,HTTP 407代理身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个.NET应用程序,在互联网上调用第三方Web服务。该服务不使用SOAP,所以我们手动构造XML请求文档,它通过HTTP发送到服务,并检索XML响应。

I'm working on a .NET app that calls 3rd party web services over the internet. The services do not use SOAP, so we manually construct an XML request document, send it to the service via HTTP, and retrieve an XML response.

我们的code是运行在一个正常的Windows域帐户的情况下Windows服务,并坐镇配置为需要NTLM身份验证的代理服务器(Microsoft ISA服务器)的后面。运行我们服务的帐户有权通过代理服务器访问互联网。

Our code is a Windows service that is run in the context of a normal Windows domain account, and sits behind a proxy server (Microsoft ISA Server) configured to require NTLM authentication. The account running our service has permission to access the internet through the proxy server.

在code是这样的:

// 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错误。

This works fine if the proxy server is turned off, or the URL has been whitelisted as not requiring authentication, but as soon as authentication is active, it always fails with an HTTP 407 error.

我把上面的code的测试工具,并想尽一切办法,我能想到的配置 request.Proxy 属性,都没有成功。

I put the above code in a test harness, and tried every method I could think of for configuring the request.Proxy property, without success.

然后我注意到,我们要呼吁所有的第三方网络服务是HTTPS。当我试图访问他们为HTTP相反,代理认证开始工作。是否有一些额外的箍我要跳,通过获取代理认证和HTTPS很好地发挥?

I then noticed that all the 3rd party web services that we have to call are HTTPS. When I tried accessing them as HTTP instead, the proxy authentication started working. Is there some extra hoop I have to jump through to get proxy authentication and HTTPS to play nicely?

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

PS: The same problems occur with the open source SmoothWall proxy server, so I can't just write it off as a bug in ISA Server.

PPS:我知道,您可以在的app.config 配置代理服务器设置,但(一)做在code不应该使任何区别,和(b)应用设计的要求,我们在运行时从数据库中读取代理服务器设置。

PPS: I'm aware that you can configure proxy settings in app.config, but (a) doing it in code shouldn't make any difference, and (b) the application design requires that we read the proxy settings from a database at runtime.

推荐答案

我想我将不得不注销了这个问题。我的原贴code确实出现有时工作。我们的代理服务器是非常不可靠的;一分钟便将阻止任何软件的互联网连接,而接下来它会允许它。 IT人员似乎无能为力吧,我们(IT部门以外的所有人)都无权进行更改的网络基础设施。

I think I will have to write off this question. My original posted code does appear to work sometimes. Our proxy server is extremely unreliable; one minute it will block an internet connection from any software, and the next it will allow it. The IT guys seem powerless to do anything about it, and we (everyone outside the IT department) have no authority to make changes to the network infrastructure.

如果任何人有如何强化我的code,以弥补不可靠的代理服务器的任何想法,那么我很想听到他们的声音。 : - )

If anyone has any ideas on how to "harden" my code to compensate for an unreliable proxy server, then I'd be interested to hear them. :-)

这篇关于调用Web服务时,HTTP 407代理身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆