System.Net.Http.HttpClient 返回错误“ERR:缺少 UA30”

问题描述 投票:0回答:1

下面的代码在响应内容中给出了相当奇怪的错误

ERR: Missing UA30
。我在互联网上的任何地方都找不到该错误。它只发生在某些特定网站上。 还有另一个版本的代码在
此存储库
上同时测试多个ics URL。 由于 F# 和 C# 共享相同的框架,我怀疑 C# 也会发生这种情况,但我还没有测试它,因为我的 C# 不太流畅。

open System.Net.Http

let url = "https://portal.macleans.school.nz/index.php/ics/school.ics"
(* other working urls:
    "https://gist.githubusercontent.com/DeMarko/6142417/raw/1cd301a5917141524b712f92c2e955e86a1add19/sample.ics"
    "https://www.kayaposoft.com/enrico/ics/v2.0?country=gbr&fromDate=01-01-2023&toDate=31-12-2023&region=eng"
    "https://www.phpclasses.org/browse/download/1/file/63438/name/example.ics"
    *)


let getAsync (client: HttpClient) (url: string) =
    async {
        let! content = client.GetStringAsync url |> Async.AwaitTask
        let len = content.Length
        printf "%d" len

        match len with
        | len when len < 50 -> printfn " content: %s" content
        | _ -> printfn ""
    }

let client = new HttpClient()

getAsync client url
|> Async.RunSynchronously

我尝试过的所有内容都已记录在上述 GitHub 存储库中。

我预计不会出现错误,因为

curl
命令能够毫无问题地下载
ics
文件 -

curl https://portal.macleans.school.nz/index.php/ics/school.ics -o output.ics
c# .net f# httpclient icalendar
1个回答
0
投票

您可以尝试在 HTTP 请求中设置用户代理设置标头。 例如:

open System.Net.Http

let url = "https://portal.macleans.school.nz/index.php/ics/school.ics"

let getAsync (client: HttpClient) (url: string) =
    async {
        let request = new HttpRequestMessage(HttpMethod.Get, url)
        request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")

        let! response = client.SendAsync(request) |> Async.AwaitTask
        let content = response.Content.ReadAsStringAsync() |> Async.AwaitTask
        printfn "Response content length: %d" (content.Length)

        if content.Length < 50 then
            printfn "Response content: %s" content
    }

let client = new HttpClient()

getAsync client url
|> Async.RunSynchronously
```
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.