Splunk Dev

how to resolve 'Unexpected DTD declaration' error when logging on using the C# sdk?

xy12345
New Member

I copied the C# Logon code from example and tried to logon to my Splunk service (http://10.134.21.107:8000). When running the C# code, I got the 'Unexpected DTD declaration' error. This error is the same with "https://answers.splunk.com/answers/518826/why-am-i-receiving-an-xmlexception-when-logging-on.html". Different from it is that I can make sure that my URL endpoint is correct since it (http://10.134.21.107:8000) can be accessed by the browser.
My code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Splunk.Client;
namespace mytest
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "10.134.21.107";
            int port = 8000;
            Scheme scheme = Scheme.Http;

            using (var service = new Service(scheme, host, port))
            {
                Console.WriteLine("Connected to {0}:{1} ", service.Context.Host, service.Context.Port);
                try
                {
                    Run(service).Wait();
                }
                catch (AggregateException ex)
                {
                    ex.Handle(exception =>
                    {

                        Console.WriteLine(exception.Message);
                        Console.WriteLine(exception.Source);
                        Console.WriteLine(exception.ToString());
                        Console.WriteLine(exception.StackTrace);
                        Console.WriteLine(exception.TargetSite.ToString());
                        return true;
                    });

                }
            }

            Console.Write("Press return to exit: ");
            Console.ReadLine();
        }


        private static async Task Run(Service service)
        {
            string username = "tester";
            string password = "Xxxx1122";
            try
            {
                await service.Configurations.GetAllAsync();
            }
            catch (AuthenticationFailureException)
            {
                Console.WriteLine("Can't get service configuration without logging in.");
            }

            await service.LogOnAsync(username, password);

            Console.WriteLine("List all configurations of the Splunk service:");
            await service.Configurations.GetAllAsync();

            foreach (Configuration config in service.Configurations)
            {
                Console.WriteLine(config.Id);
            }

            Console.WriteLine("Log off");
            await service.LogOffAsync();
        }


    }
}

The output is below:

Connected to 10.134.21.107:8000
Unexpected DTD declaration. Line 1, position 3.
System.Xml
System.Xml.XmlException: Unexpected DTD declaration. Line 1, position 3.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContentAsync()
   at System.Xml.XmlTextReaderImpl.ReadAsync()
   at System.Xml.XmlTextReaderImpl.ReadAsync_SwitchToInteractiveXmlDecl_Helper(Boolean finish)
   at System.Xml.XmlTextReaderImpl.ReadAsync_SwitchToInteractiveXmlDecl()
   at System.Xml.XmlTextReaderImpl.ReadAsync()
   at System.Xml.AsyncHelper.<_CallBoolTaskFuncWhenFinish>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.XmlReaderExtensions.<MoveToDocumentElementAsync>d__2.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\XmlReaderExtensions.cs:line 182
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.Message.<ReadMessagesAsync>d__21.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\Message.cs:line 428
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.Response.<ThrowRequestExceptionAsync>d__17.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\Response.cs:line 214
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.Response.<EnsureStatusCodeAsync>d__11.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\Response.cs:line 157
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.EntityCollection`2.<GetAllAsync>d__12.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\EntityCollection.cs:line 243
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at mytest.Program.<Run>d__1.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\mytest\Program.cs:line 51
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContentAsync()
   at System.Xml.XmlTextReaderImpl.ReadAsync()
   at System.Xml.XmlTextReaderImpl.ReadAsync_SwitchToInteractiveXmlDecl_Helper(Boolean finish)
   at System.Xml.XmlTextReaderImpl.ReadAsync_SwitchToInteractiveXmlDecl()
   at System.Xml.XmlTextReaderImpl.ReadAsync()
   at System.Xml.AsyncHelper.<_CallBoolTaskFuncWhenFinish>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.XmlReaderExtensions.<MoveToDocumentElementAsync>d__2.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\XmlReaderExtensions.cs:line 182
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.Message.<ReadMessagesAsync>d__21.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\Message.cs:line 428
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.Response.<ThrowRequestExceptionAsync>d__17.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\Response.cs:line 214
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.Response.<EnsureStatusCodeAsync>d__11.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\Response.cs:line 157
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Splunk.Client.EntityCollection`2.<GetAllAsync>d__12.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\src\Splunk.Client\Splunk\Client\EntityCollection.cs:line 243
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at mytest.Program.<Run>d__1.MoveNext() in E:\tt\ConsoleApplication3\splunk_ex\example\mytest\Program.cs:line 51
Void Throw(System.Exception)
Press return to exit:

Thanks for any help.

Labels (1)
0 Karma

ccl0utier
Splunk Employee
Splunk Employee

Could you please try using the Splunk management port (default: 8089) as in the example here? All the REST API endpoints are exposed over that port and not the default Splunk UI Web port (8000).

For example:

// Create a Service instance 
var service = new Service(Scheme.Https, "localhost", 8089, new Namespace(user: "nobody", app: "search"))

// Log in
await service.LoginAsync("admin", "yourpassword");
0 Karma

xy12345
New Member

the exception in my Splunk Answers post is thrown by this line code:
await service.Configurations.GetAllAsync();
The program has been quit before running LogOnAsync(username, password).

0 Karma
Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...