首页 > 解决方案 > 使用 Razor 读取 XML 网页时出现错误的请求 400 错误

问题描述

如标题所述,我的代码中读取 orderWebLink 并将其转换为字符串的 Html.Raw 行正在获取

错误请求 (400) 错误:
SYSTEM.NET.WEB 异常:远程服务器返回错误:(400) 错误请求

我没有显示实际的 URL,因为您必须登录到我的测试网站才能查看它生成的代码。以下是您使用浏览器访问上述 orderWebLink 时显示的代码:

This XML file does not appear to have any style information associated with it. The document tree is shown below:

    <OrderViewModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/NashWrap.Web.Models">
      <UserId>1dde2e58-dd66-4863-89be-38e36c07b3ed</UserId>
    </OrderViewModel>

我要做的就是获取 UserId 的文本值并将其放入变量中;但我似乎无法通过这个 400 错误,我无法弄清楚为什么它不会返回代码。

这样我就可以填写变量以传递给 Google 的客户评论调查。我曾尝试使用 order.Load(orderWebLink) 而不是 HTML.Raw 请求此页面,并且在此之前也尝试过使用 ajax;但得到几乎相同的问题。

@using System.Xml;

@{
    string CurrentURL = Request.Url.AbsoluteUri;
}
@if (CurrentURL.Contains("/a/ordercomplete/"))
{
    string googleOrderID = CurrentURL.Substring(CurrentURL.IndexOf("/a/ordercomplete/") + 17);
    string CurrentWebsite = CurrentURL.Substring(0, CurrentURL.IndexOf("/a/ordercomplete/"));
    string googleUserID = "";
    string googleOrderEmail = "";
    string googleOrderCountry = "";
    string googleOrderCountryCode = "US";
    string errormessage = "";
    try
    {
        string orderWebLink = CurrentWebsite + "/api/order/" + googleOrderID;
        var html = Html.Raw(new System.Net.WebClient().DownloadString(orderWebLink));
        XmlDocument order = new XmlDocument();
        order.LoadXml(html.ToString());
        foreach (XmlNode node1 in order)
        {
            googleUserID = node1["UserId"].InnerText;
        }
    }
    catch (Exception e)
    {
        errormessage = e.ToString();
    }
    @Html.Label("lblName", errormessage)
<!-- BEGIN GCR Opt-in Module Code -->
    <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer>
    </script>
    <script type="text/javascript">
        window.renderOptIn = function () {
            window.gapi.load('surveyoptin', function () {
                window.gapi.surveyoptin.render(
                    {
                        // REQUIRED
                        "merchant_id": 6316095,
                        "order_id": "@googleOrderID",
                        "email": "@googleOrderEmail",
                        "delivery_country": "@googleOrderCountryCode",
                        "estimated_delivery_date": "2021-09-20",
                        "opt_in_style": "OPT_IN_STYLE",
                        "products": [{ "gtin": "123456789123" }, { "gtin": "123456789124" }]
                        //"opt_in_style": "CENTER_DIALOG"
                    });
            });
        }
    </script>
    <!-- END GCR Opt-in Module Code -->
    <!-- BEGIN GCR Language Code -->
    <script>
        window.___gcfg = {
            lang: 'en_US'
        };
    </script>
    <!-- END GCR Language Code -->
}

标签: xmlrazor

解决方案


推荐阅读