F#:处理网络异常

F#: Handling web exceptions

我是编程新手,F# 是我的第一语言。

以下是我的代码的相关片段:

let downloadHtmlToDiskAsync (fighterHtmlDirectory: string) (fighterBaseUrl: string) (fighterId: int) = 
    let fighterUrl = fighterBaseUrl + fighterId.ToString()
    try 
        async {

            let! html = fetchHtmlAsync fighterUrl
            let fighterName = getFighterNameFromPage html

            let newTextFile = File.Create(fighterHtmlDirectory + "\" + fighterId.ToString("00000") + " " + fighterName.TrimEnd([|' '|]) + ".html")
            use file = new StreamWriter(newTextFile) 
            file.Write(html) 
            file.Close()
        }
    with
        :? System.Net.WebException -> async {File.AppendAllText("G:\User\WebScraping\Invalid Urls.txt", fighterUrl + "\n")}

let downloadFighterDatabase (directoryPath: string) (fighterBaseUrl: string) (beginningFighterId: int) (endFighterId: int) =
    let allFighterIds = [for id in beginningFighterId .. endFighterId -> id]
    allFighterIds
    |> Seq.map (fun fighterId -> downloadHtmlToDiskAsync directoryPath fighterBaseUrl fighterId)
    |> Async.Parallel
    |> Async.RunSynchronously

我已经使用 F# Interactive 测试了函数 fetchHtmlAsync 和 getFighterNameFromPage。他们都工作得很好。

但是,当我构建并 运行 解决方案时,我收到以下错误消息:

An unhandled exception of type 'System.Net.WebException' occurred in FSharp.Core.dll Additional information: The remote server returned an error: (404) Not Found.

出了什么问题?我应该做哪些改变?

把你的 try with 放在 async 里面。

let downloadHtmlToDiskAsync (fighterHtmlDirectory: string) (fighterBaseUrl: string) (fighterId: int) = 
    let fighterUrl = fighterBaseUrl + fighterId.ToString()
    async {
        try
            let! html = fetchHtmlAsync fighterUrl
            let fighterName = getFighterNameFromPage html

            let newTextFile = File.Create(fighterHtmlDirectory + "\" + fighterId.ToString("00000") + " " + fighterName.TrimEnd([|' '|]) + ".html")
            use file = new StreamWriter(newTextFile) 
            file.Write(html) 
            file.Close()
        with
            :? System.Net.WebException -> File.AppendAllText("G:\User\WebScraping\Invalid Urls.txt", fighterUrl + "\n")
    }