RestSharp Methods Throw System.Xml.XMlException “=”是一个意外的标记。预期的标记是“;”

RestSharp Methods Throw System.Xml.XMlException "=" is an unexpected token. The expected token is ';'

我查看了 的答案,发现无效字符会导致引发此错误的问题。我的问题有点不同,因为我使用 RestSharp 进行 API 调用,如下所示:

 private static T Execute<T>(IRestRequest request, string baseUrl) where T : class, new()
    {
        var client = new RestClient(baseUrl);
        var response = client.Execute<T>(request);

        if (response.ErrorException != null)
        {
            Console.WriteLine(
                "Error: Exception: {0}, Headers: {1}, Content: {2}, Status Code: {3}",
                response.ErrorException,
                response.Headers,
                response.Content,
                response.StatusCode);
        }

        return response.Data;
    }

 public static ProPayResponse MerchantSignUpForProPay()
    {
        var baseUrl = "https://xmltestapi.propay.com/ProPayAPI";
        var request = BuildMerchantTestData();
        var restRequest = CreateRestRequest("SignUp", Method.PUT);
        restRequest.AddJsonBody(request);
        return Execute<ProPayResponse>(restRequest, baseUrl);
    }

    private static async Task<RestRequest> CreateRestRequest(string resource, Method method)
    {

        var credentials = GetCredentials();

        var restRequest = new RestRequest { Resource = resource, Method = method, RequestFormat = DataFormat.Json, };
        restRequest.AddHeader("accept", "application/json");
        restRequest.AddHeader("Authorization", credentials);
        return restRequest;
    }
private static string GetCredentials()
    {
        var termId = "myterm"; // put affiliate term id here, if you have it
        var certString = "mycertString"; // put affiliate cert string here
        var encodedCredentials = Convert.ToBase64String(Encoding.Default.GetBytes(certString + ":" + termId));

        var credentials = $"Basic {encodedCredentials}";
        return credentials;
    }

异常的完整堆栈跟踪如下:

Error: Exception: System.Xml.XmlException: '=' is an unexpected token. The expected token is ';'. Line 26, position 43.
 at System.Xml.XmlTextReaderImpl.Throw(Exception e)
 at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
 at System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String expectedToken1, String expectedToken2)
 at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)
 at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
 at System.Xml.XmlTextReaderImpl.FinishPartialValue()
 at System.Xml.XmlTextReaderImpl.get_Value()
 at System.Xml.Linq.XContainer.ContentReader.ReadContentFrom(XContainer rootContainer, XmlReader r)
 at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
 at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
 at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
 at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
 at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
 at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw), Headers: System.Collections.Generic.List`1[RestSharp.Parameter], Content:

当我 运行 这段代码时,我确实注意到在堆栈跟踪的内容部分抛出了一个 HTTP 404。

我认为这意味着我有一个不正确的 baseURl 但我不确定并且想知道是否是这种情况或者我的代码是否还有其他问题?

更新: 在进一步研究这个问题之后,我认为错误被抛出是因为我在发送 RestRequest.

之前没有将我的模型对象序列化为 JSON

我是否需要在发出请求之前序列化所有对象?

更新二: 多亏了第二双眼睛,我纠正了URL。现在,当我 运行 我的应用程序时,抛出以下错误:

Error: Exception: System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw), Message: Data at the root level is invalid. Line 1, position 1., Headers: System.Collections.Generic.List`1[RestSharp.Parameter], Content: ?<?xml version="1.0" encoding="utf-8"?>

I think the error is being thrown because I'm not serializing my model objects into JSON before sending the RestRequest.

restRequest.AddJsonBody(request); 将序列化 object 并将适当的 header 添加到请求中。堆栈跟踪看起来问题在于响应作为 XML 返回,以及当它尝试反序列化时会发生什么。

When I run this code, I do note that an HTTP 404 is thrown in the content section of the stack trace.

I think this means that I have an incorrect baseURl but am not sure and would like to know if this is the case or if my code has other issues?

快速浏览一下他们的文档,您似乎在调用他们的 (SOAP) XML API。因此,如果您打算与 ProPay REST 接口交互,那么您调用了错误的基础 URL。

对于 REST,它们显示以下内容

Resource URI and HTTP Methods

The request URI is constructed from a Base URI and a Resource URI appended. A Resource URI may be used differently based on the HTTP verb of the request. Consider the following Example:

ProPay Integration environment Base URI: https://xmltestapi.propay.com
Resource: /propayAPI/signup
HTTP Method: PUT
Request Endpoint: PUT https://xmltestapi.propay.com/propayapi/signup

这意味着您需要更新代码

public static async Task<ProPayResponse> MerchantSignUpForProPay() {
    var baseUrl = "https://xmltestapi.propay.com/propayapi";
    var content = await BuildMerchantTestData();
    var request = CreateRestRequest("Signup", Method.PUT);
    request.AddJsonBody(content);
    return await Execute<ProPayResponse>(request, baseUrl);
}

private static async Task<T> Execute<T>(IRestRequest request, string baseUrl) 
    where T : class, new() {
    var client = new RestClient(baseUrl);
    var response = await client.ExecuteTaskAsync<T>(request);

    if (response.ErrorException != null) {
        Console.WriteLine(
            "Error: Exception: {0}, Headers: {1}, Content: {2}, Status Code: {3}",
            response.ErrorException,
            response.Headers,
            response.Content,
            response.StatusCode);
    }

    return response.Data;
}

private static RestRequest CreateRestRequest(string resource, Method method) {
    var credentials = GetCredentials();
    var restRequest = new RestRequest(resource, method, DataFormat.Json);
    restRequest.AddHeader("Accept", "application/json");
    restRequest.AddHeader("Authorization", credentials);
    return restRequest;
}

我建议使基础 URL 可配置而不是硬编码,这样它可以在投入生产时轻松更改而无需重新编译。

在您的 Update 2 之后,RestSharp 似乎在 XML.

的开头引入了一个意想不到的字符

这是来自错误信息:

Content: ?<?xml version="1.0" encoding="utf-8"?>

<?xml前的问号就是问题所在。它不是 XML 的有效字符,并导致 XML 解析器抛出错误。

我最好的猜测是响应中的 XML 内容在开头有一个 UTF-8 字节顺序标记 (BOM)。 BOM 在技术上不是有效字符,您的日志记录 code/framework 正在将其转换为 ? 以供显示。

您可以通过调用 .ExecuteTaskAsync(request) 而不是 .ExecuteTaskAsync<T>(request) 并查看 response.RawBytes 中返回的数据来对此进行测试。如果返回的前 3 个字节是 0xEF 0xBB 0xBF 那么您的响应中就有一个 BOM。

快速修复

这应该可以完成工作,并且需要最少的代码更改。

restRequest.OnBeforeDeserialization = resp => {
    if (resp.RawBytes.Length >= 3 && resp.RawBytes[0] == 0xEF && resp.RawBytes[1] == 0xBB && resp.RawBytes[2] == 0xBF)
    {
        // Copy the data but with the UTF-8 BOM removed.
        var newData = new byte[resp.RawBytes.Length - 3];
        Buffer.BlockCopy(resp.RawBytes, 3, newData, 0, newData.Length);
        resp.RawBytes = newData;

        // Force re-conversion to string on next access
        resp.Content = null;
    }
};

这将确保尽早删除 BOM。转成字符串XML解析时,BOM不存在

更长的修复

您可以为 XML 创建自己的反序列化器,它会检测 XML 开头的 BOM 并在解析前将其删除。这里的步骤是:

  1. 亚class RestSharp.Deserializers.XmlDeserializer。这将需要一个方法覆盖:
public override T Deserialize<T>(IRestResponse response)
{
    if (string.IsNullOrEmpty(response.Content))
        return default(T);

    if (response.Content[0] == '\uFEFF')
        response.Content = response.Content.Substring(1);

    return base.Deserialize<T>(response);
}
  1. 创建上面的一个实例class。
  2. 创建 RestSharp.Deserializers.XmlRestSerializer 的实例并使用上面步骤 2 中的 class 调用 .WithXmlDeserializer()
  3. 正在您的 RestClient 实例上调用 .AddHandler("application/xml", () => xmlRestSerializer)
    • xmlRestSerializer 是您在上面第 3 步中创建的对象。
    • 您可能需要将 application/xml 替换为其他内容,具体取决于 REST API returns.