【问题标题】:How to get title of web page using Blazor?如何使用 Blazor 获取网页标题?
【发布时间】:2020-04-26 11:34:46
【问题描述】:

我正在尝试在 Blazor 中进行应用,当我尝试获取任何网页的标题时,我收到错误 System.PlatformNotSupportedException: Operation is not supported on this platform. 还有其他解决方案比这?我也从 HttpClient 开始,但不知道如何去做,并在 CORS 授权中使用这个 get 错误。

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        var str = reader.ReadToEnd();
        Regex reg = new Regex("<title>(.*)</title>");
        MatchCollection m = reg.Matches(str);
        if (m.Count > 0)
        {
            return m[0].Value.Replace("<title>", "").Replace("</title>", "");
        }
        else
            return "";
    }

【问题讨论】:

标签: c# cors webclient blazor blazor-client-side


【解决方案1】:

使用 Blazor 服务器端,您的代码应该可以工作,尽管 HttpClient 会更好。

但是 PlatformNotSupportedException 意味着您正在 Blazor WebAssembly 上运行它。那里你必须使用 HttpClient,但是

... 使用 HttpClient [...] 获取 CORS 授权错误。

无论您使用什么,这个问题都不会消失。

Blazor WebAssembly 受到适用于任何 JavaScript 或 WebAssembly 应用程序的标准“沙盒”安全规则的限制。

您将无法随意访问网站。 API 站点可能允许使用 CORS 标头进行此操作,但您自己域之外的网站将无法访问。

【讨论】:

    猜你喜欢
    • 2021-05-23
    • 2019-11-07
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多