Webclient s downloadstring(url)从Azure函数运行时返回403,但在与Azure-Functions-Core-Tools的本地调试中正常工作

发布于 2025-02-07 18:38:02 字数 892 浏览 1 评论 0原文

我无法弄清楚为什么我的代码在本地上运行良好,而不是在Azure功能上运行。

        using (var client = new WebClient ())
        {
            log.LogInformation($"Fetching html from {url}");
            
            try
            {
               var response = client.DownloadString(url);
                baseHtmlBasePageList.Add(response);
            }
            catch(Exception ex)
            {
                log.LogError(ex.Message);
            }
        }

这是我要点击的url代码 是 https://www.redfin.com/zipcode /20171 。 我试图确定问题可能在哪里..

  1. Redfin是否能够区分来自Azure与我本地机器的请求并做出不同的反应?
  2. 我需要明确设置Azure上的某些设置吗?

代码/功能本身非常简单。它的功能像上面提到的示例一样获取URL,通过调用Downloadstring(URL)方法来获取HTML,并从返回的字符串中返回前20个字符。

我确实知道,网络客户已弃用,但是我们可以肯定地说,这就是为什么我本地机器和Azure之间行为不一致的原因?

I am not able to figure out why my code runs fine on my local but doesn't on azure functions.

This is the code snippet

        using (var client = new WebClient ())
        {
            log.LogInformation(
quot;Fetching html from {url}");
            
            try
            {
               var response = client.DownloadString(url);
                baseHtmlBasePageList.Add(response);
            }
            catch(Exception ex)
            {
                log.LogError(ex.Message);
            }
        }

The URL I am trying to hit is https://www.redfin.com/zipcode/20171.
I am trying to determine where the issue could be.. like

  1. Is Redfin able to differentiate between a request coming from Azure vs my local machine and respond differently?
  2. Is it some setting on Azure that I need to set explicitly?

The code/function itself is very simple. Its a function that takes url like the example mentioned above, fetches the html by calling the DownloadString(url) method and returns the first 20 chars from the returned string.

I do understand that WebClient is deprecated but can we say for certain that that is why the behavior is not consistent between my local machine and Azure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文