我正在尝试开发一个简单的C#应用程序,该应用程序将使用this服务(OpenSky Network)的REST功能。特别是,我对使用此命令的飞机实时跟踪感兴趣:
$ curl -s“ https://USERNAME:PASSWORD@opensky-network.org/api/tracks/all?icao24=3c4b26&time=0”
问题是,我不确定如何解释网址的“ USERNAME:PASSWORD”部分。
我已经尝试过的一些解决方案:
using(WebClient client = new WebClient())
{
var response = client.DownloadString("https://\"myusername\":\"mypassword\"@opensky-network.org/api/tracks/all?icao24=4b16bb&time=0");
}
using(WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("myusername", "mypassword");
var response = client.DownloadString("https://opensky-network.org/api/tracks/all?icao24=4b16bb&time=0");
}
WebRequest req = WebRequest.Create(@"https://opensky-network.org/api/tracks/all?icao24=4b16bb&time=0");
req.Method = "GET";
req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("myusername:mypassword"));
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
所有这些都返回以下错误:“远程服务器返回错误:(404)未找到。”。
如果密码包含无效字符(例如“ @”或“ $”),问题将变得更加严重。在这种情况下,我的代码由于无效的网址格式而引发错误。
我想念什么吗?有人可以指出我正确的方向吗?