【问题标题】:Validate an Xml file against a DTD with a proxy. C# 2.0使用代理根据 DTD 验证 Xml 文件。 C# 2.0
【发布时间】:2010-12-25 17:22:12
【问题描述】:

我查看了许多针对 DTD 验证 XML 文件的示例,但没有找到允许我使用代理的示例。我有一个 cXml 文件,如下所示(为显示而缩写),我希望对其进行验证:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.018/InvoiceDetail.dtd">
<cXML payloadID="123456" timestamp="2009-12-10T10:05:30-06:00">
    <!-- content snipped -->
</cXML>

我正在尝试创建一个简单的 C# 程序来根据 DTD 验证 xml。我尝试过如下代码,但无法弄清楚如何让它使用代理:

private static bool isValid = false;

static void Main(string[] args)
{
   try
   {
     XmlTextReader r = new XmlTextReader(args[0]);
     XmlReaderSettings settings = new XmlReaderSettings();

     XmlDocument doc = new XmlDocument();

     settings.ProhibitDtd = false;
     settings.ValidationType = ValidationType.DTD;
     settings.ValidationEventHandler +=  new ValidationEventHandler(v_ValidationEventHandler);

     XmlReader validator = XmlReader.Create(r, settings);

     while (validator.Read()) ;
     validator.Close();

     // Check whether the document is valid or invalid.
     if (isValid)
         Console.WriteLine("Document is valid");
     else
         Console.WriteLine("Document is invalid");
   }
   catch (Exception ex)
   {
     Console.WriteLine(ex.ToString());
   }
}

static void v_ValidationEventHandler(object sender, ValidationEventArgs e)
{
    isValid = false;
    Console.WriteLine("Validation event\n" + e.Message);
}

我收到的异常是

System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.

发生在while (validator.Read()) ;这一行

我知道我可以在本地根据 DTD 进行验证,但我不想更改 xml DOCTYPE,因为这是最终表单所需要的(此应用程序仅用于诊断目的)。有关 cXML 规范的更多信息,您可以转到cxml.org

感谢您的帮助。

谢谢

【问题讨论】:

    标签: c# xml validation proxy dtd


    【解决方案1】:

    你的问题已经有一段时间了,如果有点晚了,很抱歉!

    这似乎是批准的方法:

    1 - 创建您自己的代理程序集:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Configuration;
    
    namespace ProxyAssembly
    {
        public class MyProxy:IWebProxy
        {
    
    
    #region IWebProxy Members
    
        ICredentials  IWebProxy.Credentials
        {
            get 
            { 
                return new NetworkCredential(ConfigurationSettings.AppSettings["googleProxyUser"],ConfigurationSettings.AppSettings["googleProxyPassword"],ConfigurationSettings.AppSettings["googleProxyDomain"]); 
            }
            set { }
    
        }
    
        public Uri  GetProxy(Uri destination)
        {
            return new Uri(ConfigurationSettings.AppSettings["googleProxyUrl"]);
        }
    
        public bool  IsBypassed(Uri host)
        {
            return Convert.ToBoolean(ConfigurationSettings.AppSettings["bypassProxy"]);
        }
    
    #endregion
    }
    }
    

    2 - 将所需的密钥放入您的 web.config:

       <add key="googleProxyUrl"  value="http://proxy.that.com:8080"/>
        <add key="googleProxyUser"  value="service"/>
        <add key="googleProxyPassword"  value="BadDay"/>
        <add key="googleProxyDomain"  value="corporation"/>
        <add key="bypassProxy"  value="false"/>
    

    3 - 将 defaultProxy 部分放入您的 web.config 中

    <configuration>        
        <system.net>
            <defaultProxy>
                <module type="ProxyAssembly.MyProxy, ProxyAssembly"/>
            </defaultProxy>
        </system.net>    
    </configuration>
    

    现在来自您的应用程序的所有请求都将通过代理。那是 ALL 请求 - 即我认为您不能选择以编程方式使用它,每个 资源请求都会尝试通过代理!例如:使用 dtd 文档、网络服务调用等验证 xml。

    干杯, 兰斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-17
      • 2010-12-12
      • 1970-01-01
      • 2013-04-14
      • 2012-05-05
      • 2015-12-24
      • 2016-09-18
      • 1970-01-01
      相关资源
      最近更新 更多