请求通过代理时的身份验证错误

时间:2011-11-07 12:44:31

标签: c# asp.net .net networking proxy

我有以下代码通过代理服务器发出Web请求。我使用服务器上的wireshark嗅探网络流量,发现在发出请求时出现以下错误:

您的凭据无法通过身份验证:“缺少凭据。”在验证凭据之前,您不会被允许访问。\ n

身份验证应该通过NTLM运行。

有人可以帮忙吗?

    //... CALL THE CODE
    string url = String.Format("http://currencyconverter.kowabunga.net/converter.asmx/GetCultureInfo?Currency={0}", CurrencyTo.Text);
    returnValue = GetResponseValue(url);
    //...

    private static string GetResponseValue(string url)
    {
        WebRequest request = InitialiseWebRequest(url);

        WebResponse response = request.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());

        XDocument xmlDoc = new XDocument();
        xmlDoc = XDocument.Parse(sr.ReadToEnd());

        string returnValue = xmlDoc.Root.Value;
        return returnValue;
    }

    private static WebRequest InitialiseWebRequest(string url)
    {
        WebRequest request = WebRequest.Create(url);

        if (!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["proxyLogin"]))
        {
            string proxyUrl = ConfigurationSettings.AppSettings["proxyUrl"];

            if (!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["proxyPort"]))
            {
                proxyUrl += ":" + ConfigurationSettings.AppSettings["proxyPort"];
            }

            WebProxy proxy = new WebProxy(proxyUrl);

            // Create a NetworkCredential object and associate it with the Proxy property of request object.
            proxy.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["proxyLogin"], ConfigurationSettings.AppSettings["proxyPassword"]);

            NetworkCredential networkCredential = new NetworkCredential(ConfigurationSettings.AppSettings["proxyLogin"], ConfigurationSettings.AppSettings["proxyPassword"]);

            CredentialCache credentialCache = new CredentialCache();

            credentialCache.Add(new Uri(url), "NTML", networkCredential);

            request.Credentials = credentialCache;
            request.Proxy = proxy;

            return request;
        }

        return request;
    }

1 个答案:

答案 0 :(得分:0)

事实证明,用户名具有域前缀且密码已过期且未在配置中更新。

经过一整天的审视,我得到了一个灯泡时刻。