如果我设置版本,OData 元数据 url 请求失败

OData metadata url request fails if I set the version

以下代码获得 500:

WebRequest request =
  WebRequest.Create("https://services.odata.org/TripPinRESTierService/$metadata");
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.Accept = "application/atom+xml,application/xml";
request.Headers.Add("DataServiceVersion", "4.0");
request.Headers.Add("OData-MaxVersion", "4.0");
webRequest.AutomaticDecompression =
  DecompressionMethods.Deflate | DecompressionMethods.GZip;

// this throws the 500
var response = request.GetResponse();

但这成功了:

WebRequest request =
   WebRequest.Create("https://services.odata.org/TripPinRESTierService/$metadata");
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.Accept = "application/atom+xml,application/xml";
request.Headers.Add("OData-MaxVersion", "4.0");
webRequest.AutomaticDecompression =
  DecompressionMethods.Deflate | DecompressionMethods.GZip;
var response = request.GetResponse();

这会引发错误:

WebRequest request = 
  WebRequest.Create("https://services.odata.org/TripPinRESTierService/Airlines");
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.Accept = "application/atom+xml,application/xml";
request.Headers.Add("DataServiceVersion", "4.0");
request.Headers.Add("OData-MaxVersion", "4.0");
webRequest.AutomaticDecompression =
  DecompressionMethods.Deflate | DecompressionMethods.GZip;

// this throws the 500
var response = request.GetResponse();

在请求元数据时有什么特殊的地方我不应该指定 DataServiceVersion 吗?

根据 docs,OData-version 的 header 是 OData-Version,而不是 DataServiceVersion

根据我使用 Postman 的测试,这就是引发 500 错误的原因。

顺便说一句,考虑使用 HttpClient 而不是 WebClient,异步工作,并且比 WebClient 更轻。参见 this and this