我需要从 URL 中读取一些数据 - 但取决于字符串 (ICAO) - URL 有时不存在(它无效)。在这种情况下——我应该得到“N/A”——但这不起作用……只有当所有三个 URL 都可读时——它才起作用。
[Invoke]
public List<Category> getWeather(string ICAO)
{
try
{
List<Category> lstcat = new List<Category>();
Category cat = new Category();
string fileString;
bool isexists = FtpDirectoryExists("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/" + ICAO);
if (isexists == true)
{
WebClient request = new WebClient();
string url = "http://weather.noaa.gov/pub/data/observations/metar/stations/" + ICAO;
byte[] newFileData = request.DownloadData(url);
fileString = System.Text.Encoding.UTF8.GetString(newFileData);
cat.Cat = "METAR";
lstcat.Add(cat);
cat = new Category();
cat.Cat = fileString;
lstcat.Add(cat);
url = "http://weather.noaa.gov/pub/data/forecasts/shorttaf/stations/" + ICAO;
newFileData = request.DownloadData(url);
fileString = System.Text.Encoding.UTF8.GetString(newFileData);
cat = new Category();
cat.Cat = "Short TAF";
lstcat.Add(cat);
cat = new Category();
cat.Cat = fileString;
lstcat.Add(cat);
url = "http://weather.noaa.gov/pub/data/forecasts/taf/stations/" + ICAO;
newFileData = request.DownloadData(url);
fileString = System.Text.Encoding.UTF8.GetString(newFileData);
cat = new Category();
cat.Cat = "Long TAF";
lstcat.Add(cat);
cat = new Category();
cat.Cat = fileString;
lstcat.Add(cat);
}
else
{
fileString = "N/A;N/A";
}
return null;
}
catch (Exception)
{
throw;
}
}