0

我需要从 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;
        }
    }
4

2 回答 2

1

创建一个方法来检查远程文件是否存在。向 URl 发出 Heder 请求,如果 sattus 代码为 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()
    }
}
于 2013-09-07T06:41:24.833 回答
0

好吧,我想我明白你想做什么了。我重新编写了您的代码,使其符合实际需要。

现在,似乎第一个Category输入lstcat具有第一个 URL 的描述(例如“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));
}

我认为这以一种更干净、更直接的方式完成了你想要的。请让我知道是否确实如此!

于 2013-09-08T16:44:17.477 回答