简体   繁体   English

使用我们的应用程序打开SVG时DTD上的错误403(禁止)

[英]Error 403 (Forbidden) on the DTD while opening a SVG with our application

In our C# application, sometimes the app needs to open a svg file. 在我们的C#应用程序中,有时应用程序需要打开一个svg文件。 To do so, the following code is used: 为此,使用以下代码:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.None;
XmlReader xr = XmlReader.Create(serverMainPath + @"/path/to/svg/file.svg", settings);
XmlDocument xd = new XmlDocument();
_nmsm = new XmlNamespaceManager(xd.NameTable);
_nmsm.AddNamespace("svg", "http://www.w3.org/2000/svg");
_nmsm.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/SVGExtensions/");
xd.Load(xr);

The standard svg we're using starts with (and as far as I know it's a valid svg): 我们使用的标准svg开头(据我所知它是一个有效的svg):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
    <!-- Généré par Microsoft Visio 11.0, SVG Export, v1.0 svgIncluded.svg Page-1 -->
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:v="http://schemas.microsoft.com/visio/2003/SVGExtensions/" width="8.26772in" height="11.6929in" viewBox="0 0 595.276 841.889" xml:space="preserve" color-interpolation-filters="sRGB" class="st23" >

And it worked just fine during the last 6 months. 在过去的6个月里它运作得很好。 But since one or two weeks, this code sometimes produced the following error: 但是,由于一到两周,此代码有时会产生以下错误:

Server Error in '/ourApp' Application.
--------------------------------------------------------------------------------

The remote server returned an error: (403) Forbidden. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.WebException: The remote server returned an error: (403) Forbidden.

Source Error: 

Line 40:                 _nmsm.AddNamespace("svg", "http://www.w3.org/2000/svg");
Line 41:                 _nmsm.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/SVGExtensions/");
Line 42:                 xd.Load(xr);


Source File: path/to/the/file/file.cs    Line: 42 

Stack Trace: 

[WebException: The remote server returned an error: (403) Forbidden.]
   System.Net.HttpWebRequest.GetResponse() +5400333
   System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69
   System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929515
   System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54
   System.Xml.XmlTextReaderImpl.OpenStream(Uri uri) +34
   System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +380

[XmlException: An error has occurred while opening external DTD 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd': The remote server returned an error: (403) Forbidden.]
   System.Xml.XmlTextReaderImpl.Throw(Exception e) +76
   System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +513
   System.Xml.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId) +16
   System.Xml.DtdParser.ParseExternalSubset() +21
   System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset) +4017069
   System.Xml.DtdParser.Parse(Boolean saveInternalSubset) +54
   System.Xml.DtdParserProxy.Parse(Boolean saveInternalSubset) +31
   System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() +254
   System.Xml.XmlTextReaderImpl.ParseDocumentContent() +451
   System.Xml.XmlTextReaderImpl.Read() +151
   System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) +58
   System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +20
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +129
   System.Xml.XmlDocument.Load(XmlReader reader) +108
   OurMethod(params) in path/to/the/file/file.cs:42
   StackTrace in our App.

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3643; ASP.NET Version:2.0.50727.3634 

At first I thought it was due to a change in our internet proxy policy, but since it has been spotted on another deployment of the app which is behind a different proxy, I highly doubt that. 起初我认为这是由于我们的互联网代理政策发生了变化,但由于它已经被发现在另一个不同代理的应用程序部署中,我非常怀疑。

The error seems to be more or less random, sometimes it'll work, sometimes it won't. 错误似乎或多或少随机,有时它会起作用,有时它不会。 I'm just clueless at why it would act this way. 我只是不知道为什么会这样做。

The question is: Do you have any idea why would this happen? 问题是:你知道为什么会发生这种情况吗? If yes do you know how to fix it? 如果是,你知道如何解决它吗? If no, any idea of a workaround? 如果不是,任何解决方法的想法?

In the end, I used a homemade class to do the job. 最后,我用了一个自制的班级来完成这项工作。 It's inspired from the MSDN implementation of the XmlUrlResolver extension . 它的灵感来自XmlUrlResolver扩展MSDN实现 Thanks to @mzjn for pointing out a link that pointed out to the MSDN ressource. 感谢@mzjn指出了指向MSDN资源的链接。

using System;
using System.Net;
using System.Net.Cache;
using System.Xml;
using System.IO;

namespace My.Nasmespace.Class.IO
{
    class XmlExtendedResolver : XmlUrlResolver
    {
        bool enableHttpCaching;
        ICredentials credentials;
        private string localServerPath;
        //resolve ressource from localServerPath if it's possible
        //resolve resources from cache (if possible) when enableHttpCaching is set to true 
        //resolve resources from source when enableHttpcaching is set to false  
        public XmlExtendedResolver(bool enableHttpCaching, string serverPath)
        {
            this.enableHttpCaching = enableHttpCaching;
            localServerPath = serverPath;
        }

        public override ICredentials Credentials
        {
            set
            {
                credentials = value;
                base.Credentials = value;
            }
        }

        public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            if (absoluteUri == null)
            {
                throw new ArgumentNullException("absoluteUri");
            }
            //resolve resources from cache (if possible) 
            if (absoluteUri.Scheme == "http" && enableHttpCaching && (ofObjectToReturn == null || ofObjectToReturn == typeof(Stream)))
            {
                /*Try to resolve it from the local path if it exists*/
                if (!string.IsNullOrEmpty(localServerPath) && absoluteUri.AbsolutePath.EndsWith(".dtd") && !absoluteUri.AbsoluteUri.StartsWith(localServerPath))
                {
                    try { 
                        return GetEntity(new Uri(localServerPath + "/Xml/" + absoluteUri.Segments[absoluteUri.Segments.Length-1]), role, ofObjectToReturn);
                    } catch (FileNotFoundException){
                         //Fail silently to go to the web request.
                    }
                }
                WebRequest webReq = WebRequest.Create(absoluteUri);
                webReq.CachePolicy = new    HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
                if (credentials != null)
                {
                    webReq.Credentials = credentials;
                }
                WebResponse resp = webReq.GetResponse();
                return resp.GetResponseStream();
            }
            //otherwise use the default behavior of the XmlUrlResolver class (resolve resources from source) 
        else
            {
                return base.GetEntity(absoluteUri, role, ofObjectToReturn);
            }
        }
    }
}

i think you have to try some logics around this , i just buffered from some where 我认为你必须尝试一些逻辑,我只是​​从一些地方缓冲

request.UserAgent = "userAgents";
request.Accept = "*/*";

and

request.Credentials = new NetworkCredentials("username", "password");

or 

 request.Credentials = CredentialCache.DefaultCredentials;

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

相关问题 托管我们的 ASP.NET 核心 MVC 正在返回此错误“尝试使用 ErrorDocument 处理请求时遇到 403 禁止错误..” - Host our ASP.NET Core MVC is returning this error "403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.." 在 PuppeteerSharp 中初始化 BrowserFetcher 时引发 403 禁止错误 - 403 forbidden error raised while Initializing BrowserFetcher in PuppeteerSharp Sharepoint 2013基于表单的身份验证应用程序禁止错误403 - Sharepoint 2013 Forms Based Authentication Application Forbidden error 403 错误 - 403 禁止 - Microsoft-Azure-Application-Gateway/v2 - Error - 403 Forbidden - Microsoft-Azure-Application-Gateway/v2 在根文件夹下获取 javascripts 时出现 403 禁止错误 - 403 forbidden error while getting javascripts under root folder 远程服务器(403)使用WebClient时出现禁止错误 - Remote server (403) Forbidden error while using WebClient 向Twitter API发送请求时被禁止(错误403) - Forbidden (error 403) while sending request to Twitter API HttpWebRequest返回“(403)Forbidden”错误 - HttpWebRequest returns “ (403) Forbidden” Error 403 禁止错误 REST 调用 - 403 Forbidden error REST Call Web服务:403禁止错误 - Web Service: 403 Forbidden error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM