gpt4 book ai didi

cefsharp - 如何使用 CefSharp 的自定义方案将 'Access-Control-Allow-Origin' 添加到请求的资源

转载 作者:行者123 更新时间:2023-12-05 06:28:21 24 4
gpt4 key购买 nike

使用 XMLHttpRequest which is the subject of my previous question使用自定义方案我正在加载本地文件。为了测试,我使用了 disable-web-security 参数,但我不想使用禁用此功能的浏览器。

启用 disable-web-security 然后我得到

No 'Access-Control-Allow-Origin' header is present on the requested resource.

如何(或在哪里)将其添加到页眉或以其他方式修复它。

首先,我尝试使用 AddCrossOriginWhitelistEntry,但我不知道要输入什么。我的方案是 test://,我调用例如 test://local/folder/3Markets.xlsx。网页来源是https://product.company.de

CefSharp.Cef.AddCrossOriginWhitelistEntry("test://local", "test", "product.company.de", True)

还有这个

CefSharp.Cef.AddCrossOriginWhitelistEntry("test://local", "test", "", True)

但老实说,我不明白我应该放在这里的是什么。

我还认为我可以通过自定义方案处理来解决这个问题。

Dim cs As New CefCustomScheme With {
.SchemeName = DBSchemeHandler.DBSchemeName,
.SchemeHandlerFactory = New DBSchemeHandler,
.IsStandard = True, 'DONT THINK THIS IS NEEDED
.IsCorsEnabled = True 'DONT THINK THIS IS NEEDED
}
dbSurferSettings.RegisterScheme(cs)

我想在响应中我需要添加这个标题。还是要求?

Public Overrides Function GetResponse(response As IResponse, ByRef responseLength As Long, ByRef redirectUrl As String) As Stream

Dim aURI As New Uri(response)
Dim fileName As String = aURI.AbsolutePath

Dim bytes As Byte() = File.ReadAllBytes(fileName)
Dim mStream = New MemoryStream(bytes)

If mStream Is Nothing Then
Return Nothing
Else
Stream = mStream
Stream.Position = 0
responseLength = Stream.Length
Dim fileExtension = Path.GetExtension(fileName)
MimeType = GetMimeType(fileExtension)
StatusCode = CInt(HttpStatusCode.OK)
response.Headers.Add("Access-Control-Allow-Origin", "*")
Return mStream
End If
End Function

最佳答案

对于评论此内容的任何人。 Amaitland 的评论纠正了我的问题。

网站的域名是https://product.company.de我的计划是测试。因此,一种方法如下。

CefSharp.Cef.AddCrossOriginWhitelistEntry("https://product.company.de", "test", "", True)

我做错的另一件事是我在应用程序启动时添加了它。但它返回错误。一旦浏览器实际位于网站上,您就需要添加它,在我的示例中为 https://product.company.de .在启动时将其与所有其他设置一起设置意味着它返回下降。我已将其放入 FrameLoadEnd 事件中。

关于cefsharp - 如何使用 CefSharp 的自定义方案将 'Access-Control-Allow-Origin' 添加到请求的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54398446/

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