我需要从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;
}
}
答案 0 :(得分:1)
创建检查远程文件是否存在的方法。向URl发出Header请求,如果状态代码为200或302,则返回true,否则为false;
HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(/* url */);
request.Method = "HEAD";
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
/* A WebException will be thrown if the status of the response is not `200 OK` */
}
finally
{
// Don't forget to close your response.
if (response != null)
{
response.Close()
}
}
答案 1 :(得分:0)
好的,我想我明白了你想做什么。我重新编写了代码,使其符合实际需要。
现在,Category
中的第一个lstcat
似乎有第一个网址的描述(例如“METAR”),Category
中的第二个lstcat
有fileString
匹配它,依此类推。这非常模糊和繁琐。相反,要使一个Category
对象包含您需要了解的有关URL的所有内容:
public class Category
{
public string description;
public string fileString;
//Other fields you might use somewhere else...
public Category(string description, string fileString /*, other fields, if any...*/)
{
this.description = description;
this.fileString = fileString;
//Initialize others...
}
}
然后我将所有URL下载代码放入一个单独的函数中,从而消除了原始代码中的所有重复。
[Invoke]
public List<Category> getWeather(string ICAO)
{
bool isexists = FtpDirectoryExists("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/" + ICAO);
if (isexists)
{
List<Category> lstcat = new List<Category>();
addCategoriesToList(
lstcat,
"http://weather.noaa.gov/pub/data/observations/metar/stations/" + ICAO,
"METAR"
);
addCategoriesToList(
lstcat,
"http://weather.noaa.gov/pub/data/forecasts/shorttaf/stations/" + ICAO,
"Short TAF"
);
addCategoriesToList(
lstcat,
"http://weather.noaa.gov/pub/data/forecasts/taf/stations/" + ICAO,
"Long TAF"
);
return lstcat;
}
else
{
return null;
}
}
private static void addCategoriesToList(List<Category> lstcat, string url, string description)
{
string fileString;
//Use "using" so that `request` always gets cleaned-up:
using (WebClient request = new WebClient())
{
try
{
byte[] newFileData = request.DownloadData(url);
fileString = System.Text.Encoding.UTF8.GetString(newFileData);
}
catch
{
fileString = "N/A";
}
}
lstcat.Add(new Category(description, fileString));
}
我认为这可以更清洁,更直接的方式实现您的目标。如果确实如此,请告诉我!