首页 > 解决方案 > 我的简单 C# HTTP 服务器:嵌入式 HTML 链接在 Edge 浏览器中不起作用

问题描述

我有一个基本的 http 文件服务器,它带有一个用 WPF/C# 编写的简单 API,供用户在本地或从他们的本地网络访问。它使用System.Net.HttpListener。所以加载 jpg 的 URL 例如:

http://localhost:8085/?getfile=d:/temp/Patern_test.jpg

在包括 Edge 在内的所有浏览器中都能完美运行。没问题。但是,如果我将任何指向我的文件服务器的链接嵌入到 HTML 文件中:

<html> 
  <body> 
    <object data="http://localhost:8085/?getfile=d:/temp/Patern_test.jpg">
    </object>    
   </body>
</html>

然后它只适用于 Chrome 和 Firefox。不是 Edge 或 IE 11。我尝试了图像、视频和 iframe 标签,结果是一样的。我尝试用实际 IP 替换 localhost 并远程调用它,结果相同。

但是,如果我将链接嵌入到另一个站点而不是我的服务器,它在所有浏览器上都可以正常工作:

<html> 
  <body> 
    <object data="https://upload.wikimedia.org/wikipedia/commons/d/db/Patern_test.jpg">
    </object>
  </body>
</html>

因此,Edge 似乎不喜欢我的服务器。谁能帮我理解这可能是什么?这是标题的比较:

我的本地 HTTP 服务器(Edge 不喜欢这样):

Request URL: http://localhost:8085/?getfile=d:/temp/Patern_test.jpg
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:8085
Referrer Policy: no-referrer-when-downgrade

Response Headers

HTTP/1.1 200 OK
Content-Length: 36623
Content-Type: image/jpg
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 30 Aug 2018 10:50:03 GMT


Request Headers

GET /?getfile=d:/temp/Patern_test.jpg HTTP/1.1
Host: localhost:8085
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

维基百科服务器(Edge 喜欢这个):

Request URL: https://upload.wikimedia.org/wikipedia/commons/d/db/Patern_test.jpg
Request Method: GET
Status Code: 304 
Remote Address: 198.35.26.112:443
Referrer Policy: no-referrer-when-downgrade

Response Headers

accept-ranges: bytes
access-control-allow-origin: *
access-control-expose-headers: Age, Date, Content-Length, Content-Range, X-Content-Duration, X-Cache, X-Varnish
age: 39865
content-type: image/jpeg
date: Thu, 30 Aug 2018 11:12:24 GMT
etag: 77d2a6bf840622331df62963174df72d
last-modified: Mon, 07 Oct 2013 13:46:55 GMT
status: 304
strict-transport-security: max-age=106384710; includeSubDomains; preload
timing-allow-origin: *
via: 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1)
x-analytics: https=1
x-cache: cp1090 hit/1, cp2022 pass, cp4021 hit/3, cp4021 miss
x-cache-status: hit-local
x-client-ip: 47.154.205.235
x-object-meta-sha1base36: 8ktqmjes2nx4xcpy5h6v7o1sh6yao09
x-timestamp: 1381153614.85319
x-trans-id: tx76f1f3c2f9384f9eaff9b-005b871d52
x-varnish: 207150442 204346683, 73416305, 38868466 25417638, 135979082


Request Headers

:authority: upload.wikimedia.org
:method: GET
:path: /wikipedia/commons/d/db/Patern_test.jpg
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: max-age=0
cookie: WMF-Last-Access-Global=26-Aug-2018; GeoIP=US:CA:Thousand_Oaks:34.21:-118.88:v4
dnt: 1
if-modified-since: Mon, 07 Oct 2013 13:46:55 GMT
if-none-match: 77d2a6bf840622331df62963174df72d
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36

另外我应该提到,我非常需要它在 Edge 中工作,因为它将与 UWP 应用程序中的 WebView 控件一起使用。

更新

事实证明,这个问题只发生在两种情况下,这两种情况对我来说都不重要:

  1. 该链接嵌入在我直接在 Edge 中加载的本地 html 文件中。
  2. 如果我使用 UWP 的 WebView.NavigateToString(stringOfHtml)。

如果 HTML 是使用 WebView.Navigate(Uri) 直接从本地服务器提供的,则嵌入式链接可以正常工作。

这让我怀疑这是为了防止动态创建的 html 运行恶意代码而采取的某种低级安全措施。只是一个想法。

标签: c#httpcross-browsermicrosoft-edgehttplistener

解决方案


推荐阅读