How to fetch html from the website using WebClient in Azure Functions?

krishna572 876 Reputation points
2022-06-24T14:19:03.653+00:00

How to fetch the HTML Code from the website using Azure Functions, below code I have tried but not getting the result!

   CSharp  
   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);  
               }  
           }  
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,054 questions
0 comments No comments
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,506 Reputation points
    2022-07-06T05:49:41.663+00:00

    @NFSCoder-9821 Thanks for reaching out and apology for the delay. The query is more towards C# rather than azure function. I have leveraged the code below and I can see the HTML text return from my test webpage. I am not sure what the type of baseHtmlBasePageList that you have defined in your code.

    #r "Newtonsoft.Json"  
      
    using System.Net;  
    using Microsoft.AspNetCore.Mvc;  
    using Microsoft.Extensions.Primitives;  
    using Newtonsoft.Json;  
    using System.Net.Http.Headers;  
      
      
    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)  
    {  
        log.LogInformation("C# HTTP trigger function processed a request.");  
       string url = "https://google.com";  
    HttpClient client = new HttpClient();  
    using HttpResponseMessage response = await client.GetAsync(url);  
    using HttpContent content = response.Content;  
    string pageContent = await content.ReadAsStringAsync();  
        return new OkObjectResult(pageContent);  
      
    }  
    

    218006-image.png

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.