不是预期的" /> XML 文档 (1,2) 中存在错误,System.InvalidOperationException: <AuthorizationResult xlms:""> 不是预期的

XML 文档 (1,2) 中存在错误,System.InvalidOperationException: 不是预期的

2023-12-24

从 API 发送的 XML

<AuthenticationResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<PAPIErrorCode>0</PAPIErrorCode>
<ErrorMessage/>
<AccessToken>StringAccessToken</AccessToken>
<AccessSecret>StringAccessToken</AccessSecret>
<PolarisUserID>PolarisSampleUser</PolarisUserID>
<BranchID>7</BranchID>
<AuthExpDate>2013-05-27T16:57:46.323</AuthExpDate>
</AuthenticationResult>

响应类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace PAPIAutomatedTestingTool
{
    [XmlRoot(ElementName="AuthorizationResult")]
    public class AuthorizationResult
    {
        public int PAPIErrorCode { get; set; }

        public string ErrorMessage { get; set; }

        public string AccessToken { get; set; }

        public string AccessSecret { get; set; }

        public int PolarisUserID { get; set; }

        public int BranchID { get; set; }

        public DateTime AuthExpDate { get; set; }
    }
}

代码发出请求并反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using System.Xml;
using System.Xml.Serialization;

namespace PAPIAutomatedTestingTool
{
    public class APICallMethods
    {
        public string URI { get; set; }
        public string accSecret { get; set; }
        public string accToken { get; set; }
        public string authorizationString { get; set; }

        public bool AuthenticateStaffUser()
        {
            try
            {

            //Initializing all variables
            string authReqMethod = "POST";
            string authAccessKey = "Sample Access Key";
            string authAccessKeyID = "Sample Access ID";
            string authPatronPassword = "";
            DateTime authDateTime = DateTime.Now;
            string httpAuthDateTime = authDateTime.ToUniversalTime().ToString("r");
            string authURI = "Sample URI";


            //Composing the papiHash from the given parameters
            string papiHash = GetPAPIHash(authAccessKey, authReqMethod, authURI, httpAuthDateTime, authPatronPassword);
            //Formating the authorization string 
            string authorizationString = String.Format("Authorization: PWS {0}:{1}", authAccessKeyID, papiHash);


            //Creating and defining the WebRequest
            WebRequest req = WebRequest.Create(authURI);
            req.Method = "POST";
            req.Headers.Add("PolarisDate", httpAuthDateTime);
            req.Headers.Add(authorizationString);
            req.ContentType = "application/xml";
            string requestBody = "<AuthenticationData><Domain>SampleDomain</Domain><Username>SampleUsername</Username><Password>SamplePassword</Password></AuthenticationData>";
            byte[] reqBodyBytes = System.Text.Encoding.UTF8.GetBytes(requestBody);
            req.ContentLength = reqBodyBytes.Length;
            using (Stream requestStream = req.GetRequestStream())
            {
                requestStream.Write(reqBodyBytes, 0, reqBodyBytes.Length);
            }


            //Receiving the WebResponse
            using (WebResponse resp = req.GetResponse())
            {
                AuthorizationResult firstResponse = new AuthorizationResult();
                Stream respStream = resp.GetResponseStream();
                StreamReader sr = new StreamReader(respStream);


                XmlSerializer xmlSerializer = new XmlSerializer(typeof(AuthorizationResult));
                firstResponse = (AuthorizationResult)xmlSerializer.Deserialize(respStream);
                Console.WriteLine("Authorization: PWS" + firstResponse.AccessSecret + ":" + firstResponse.AccessToken);
                return true;
            }
        }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }

        }


        public string GetPAPIHash(string strAccessKey, string strHTTPMethod, string strURI, string strHTTPDate, string strPatronPassword)
        {
            byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(strAccessKey);
            HMACSHA1 hmac = new HMACSHA1(secretBytes);

            byte[] dataBytes = null;

            if (strPatronPassword.Length > 0)
            {
                dataBytes = UTF8Encoding.UTF8.GetBytes(strHTTPMethod + strURI + strHTTPDate + strPatronPassword);
            }
            else
            {
                dataBytes = UTF8Encoding.UTF8.GetBytes(strHTTPMethod + strURI + strHTTPDate);
            }
            byte[] computedHash = hmac.ComputeHash(dataBytes);
            string computedHashString = Convert.ToBase64String(computedHash);

            return computedHashString;
        }
    }
}

我正在向 API 发出 POST 请求,其正文包含 AuthorizationData。 API 应该返回 xml 以反序列化为firstresponse。我已收到 xml(通过打印到控制台确认),但我收到的是There is an error in the XML Document(1,2) and <AuthorizationData xmlns="">没想到。我在这里先向您的帮助表示感谢。


它似乎在抱怨一个意外的根元素,并且在您的问题的其他地方存在一些混乱。

在问题标题中你有<AuthorizationResult>

在示例 XML 响应中,您有<AuthenticationResult>

在 C# 中你有[XmlRoot(ElementName="AuthorizationResult")]

在你的问题最后你提到<AuthorizationData>

这是一个主题的一些微妙的变化。

假设 XML 示例响应是正确的,您应该更改 C# 以期望根元素是<AuthenticationResult>...

[XmlRoot(ElementName="AuthenticationResult")]
public class AuthenticationResult
{
    ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XML 文档 (1,2) 中存在错误,System.InvalidOperationException: 不是预期的 的相关文章

随机推荐

  • 修改内容配置文件表单以显示矩阵中的复选框

    我需要像这样显示矩阵http eugen gotdns com test zeitplaner png http eugen gotdns com test zeitplaner png以内容简介的形式 我可以用CSS以这种方式排列复选框
  • 通过git hook执行python子进程

    我在 Git 存储库上运行 Gitolite 并且我有用 Python 编写的 post receive 挂钩 我需要在 git 存储库目录中执行 git 命令 有几行代码 proc subprocess Popen git log n1
  • T-sql - 确定值是否为整数

    我想确定一个值是否为整数 例如TryParse在 NET中 很遗憾ISNUMERIC不适合我 因为我只想解析整数而不是每种数字 有没有这样的事情ISINT或者其他的东西 这里有一些代码可以让事情变得清楚 如果MY FIELD不是 int 此
  • C 语言中 OpenMP 静态调度和动态调度的区别

    我有两个类似的代码 First pragma omp parallel for shared g private i schedule dynamic 1 for i g actualNumberOfChromosomes i lt g m
  • 导入错误:无法导入名称“_safe_split”

    当我尝试使用 train test split 函数时出现以下错误 然后我尝试安装scipy 但没有帮助 有谁知道我可能从下面的错误中遗漏了哪些内容 谢谢 ImportError Traceback most recent call las
  • Vite + React Docker

    我正在尝试使用 docker 容器运行我的 vite react 应用程序 代码运行良好 但不幸的是 它没有在 localhost 3000 中打开 Docker文件 FROM node 18 alpine EXPOSE 3000 WORK
  • 在 Ruby 中设置请求标头

    我有其余的客户端 gem 我正在定义这样的请求 url http someurl request data gt data to json response RestClient post url request content type
  • 将 NSString 解析为 JSON

    我读过几个论坛 但似乎无法完成这个简单的任务 我在 Xcode 中有一个视图 它指向 PHP 脚本并将结果存储为下面的 NSString id 16 name 鲍勃 age 37 我在解析这个 NSString 时遇到问题 这就是我获取 N
  • IBM MQ 8 的 JMeter 配置

    我正在尝试使用 JMeter 测试 IBM MQ 8 0 有人有我需要的各种参数的示例 也许是屏幕截图 吗 我找到了几个例子 但非常不一致 更准确地说 我应该填写什么 队列连接工厂 JNDI 名称请求队列 一些示例显示队列名称 有些是 JN
  • 使用 NLTK 和 Python 创建自定义分类语料库

    我遇到了一些与正则表达式有关的问题CategorizedPlaintextCorpusReader在Python中 我想创建一个自定义分类语料库并在其上训练朴素贝叶斯分类器 我的问题如下 我想要两个类别 pos 和 neg 正片文件都在一个
  • 更改 netbeans 中自动突出显示的变量名称的突出显示颜色?

    当我移动类名称时 该类名称显示的所有其他位置都会突出显示 当我将鼠标悬停在变量上时 它会在我使用该变量的不同位置执行相同的操作 我找不到在 Netbeans 中更改该设置的位置 我更改了其他所有内容 但即使当我查看突出显示部分中的每个选项时
  • Xml 架构规范是否定义了具有默认值的可选属性的序列化?

    首先 Xml Schema 规范does指定parsing of an optional attribute with a default value 如果 xml 文档中不存在这样的属性 那么当使用 XSD 进行解析时 它们似乎存在 并且
  • 对 Laravel 5 发送邮件队列感到困惑 (Laravel 5.4)

    我从 Laravel 升级4 1 to 5 4 队列迁移很困难 首先我注释掉了iron来自路线的路线 Route post queue receive function return Queue marshal 然后 我配置了数据库驱动程序
  • 如何使 dotnet 命令成为 docker 中的第二个命令?

    这是我的CMD我的 Dockerfile 中的指令 CMD bin bash c dotnet Project Api dll 当我使用docker exec it为了在我的容器中进行交互式bash 我看到了dotnetPID为1 ps e
  • 一次更新sql server xml数据字段上的多个属性

    是否可以在一次更新中修改单个节点上的多个属性 我有类似的东西
  • Java.Util.Scanner 的 NoSuchElementException

    我对 Java 很陌生 但正在阅读 Java 如何编程 第 9 版 一书 并找到了一个例子 我一生都无法弄清楚问题是什么 这是教科书中源代码示例的 稍微 增强版本 import java util Scanner public class
  • 从依赖于外部库的终端运行 java

    我已经阅读了很多 但还没有找到解决方案 我在用着lwjgl 它需要 2 个 jar 和一个本机库才能运行 lwjgl jar lwjgl util jar和当地人图书馆 我已经尝试过我能想到的各种方法 无论如何 我现在正在尝试使用这样的命令
  • 如何在 Perl 中实时暂停

    我对 Perl 还很陌生 这是我在这里发表的第一篇文章 所以请保持温柔 我在 60 秒的倒计时计时器中使用实时 需要能够每 10 秒停止一次 然后用户必须输入是否要在每个 10 秒的间隔继续倒计时 计时器工作 我只是不知道如何暂停它以便用户
  • 如何使用 jndi 显示 ldap 目录的所有对象类描述

    我想显示 LDAP 目录架构中存在的所有对象类 以帮助用户输入可用的对象类以添加新条目 DirContext schema ctx getSchema Attributes answer schema getAttributes Class
  • XML 文档 (1,2) 中存在错误,System.InvalidOperationException: 不是预期的

    从 API 发送的 XML