【问题标题】:Why does HttpRequest not work on Windows but works on Mac?为什么 HttpRequest 不能在 Windows 上工作,但在 Mac 上工作?
【发布时间】:2019-06-01 13:23:36
【问题描述】:

我使用 Mac 的伙伴可以毫无问题地运行该程序,但是当我尝试运行它时,它会出现问题

WebException:远程服务器返回错误:(400) Bad Request。

我尝试复制请求 URL(在构建字符串之后)并且浏览器给了我预期的答案。

代码是这样的:

double SearchLatitude = 41.480687;
double SearchLongitude = -8.527346;
double Radius = 10; // km  
int maxResults = 5;
string bingMapsKey = "MyKey";
string requestUrl = string.Format("http://spatial.virtualearth.net/REST/v1/data/c2ae584bbccc4916a0acf75d1e6947b4/NavteqEU/NavteqPOIs" +
            "?spatialFilter=nearby({0},{1},{2})&$filter=EntityTypeID Eq 5400&$top={3}&key={4}", SearchLatitude, SearchLongitude, Radius, maxResults,bingMapsKey);

HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse; 

这可能是 Windows 问题吗?

【问题讨论】:

  • 如果那是你真正的 bingMapsKey 那么你就不应该发布它。
  • @HenkHolterman 不,不是
  • @HenkHolterman 它已修改,但我可以将其更改为看起来不真实

标签: c# .net-core httprequest


【解决方案1】:

首先要检查的是区域设置(1.2 与 1,2)。

而且总是最好用

//string requestUrl = string.Format("http://spatial.virtualearth.net/REST/...", ...);
  string requestUrl = string.Format(CultureInfo.InvariantCulture, "http://spatial.virtualearth.net/REST/...", ...); 

【讨论】:

  • 谢谢!但是你能解释一下每种方法之间的区别吗?
  • 好吧,看看最初在 Mac 和 Windws PC 上生成的确切 URL。他们会有所不同。只有一个是对的。
猜你喜欢
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多