为什么HttpRequest在Windows上不起作用,而在Mac上却起作用?

时间:2019-06-01 13:23:36

标签: c# .net httprequest core

我的使用Mac的伴侣可以毫无问题地运行该程序,但是当我尝试运行该程序时,它将显示WebException: The remote server returned an error: (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的问题吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

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

始终最好使用

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