was not expected-6ren"> was not expected-从 API 发送的 XML 0 StringAccessToken StringAccessToken PolarisSampleUser 7 2013-05-27T16:57:46.323 响-6ren">
gpt4 book ai didi

c# - XML 文档 (1,2) 中存在错误,System.InvalidOperationException : was not expected

转载 作者:可可西里 更新时间:2023-11-01 08:21:24 24 4
gpt4 key购买 nike

从 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;
}
}
}

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

最佳答案

它似乎在提示一个意外的根元素,并且在您的问题的其他地方围绕着它存在一些混淆。

在问题标题中你有 <AuthorizationResult>

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

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

在您的问题末尾,您提到了 <AuthorizationData>

这是一个主题的相当多的细微变化。

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

[XmlRoot(ElementName="AuthenticationResult")]
public class AuthenticationResult
{
...

关于c# - XML 文档 (1,2) 中存在错误,System.InvalidOperationException : <AuthorizationResult xlms :""> was not expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16763852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com