1

I keep getting a 403 error when i run this code has anyone got an idea how I can fix this I tried some of the other fixes posted to this site but they didn't seem to work any help would be appreciated

using System;
using System.Net;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient client = new WebClient())
            {
                Console.WriteLine(client.DownloadString("http://www.bom.gov.au/vic/forecasts/ballarat.shtml"));
            }
        }
    }
}
zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • 1
    Try adding this BEFORE the `console.writeline` `client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.");` – zaitsman Aug 07 '20 at 00:45

1 Answers1

3

They ban some user agents, so you need to pretend to be a browser or something.

using (var client = new WebClient())
{
    client.Headers.Add(HttpRequestHeader.UserAgent, "/");
    Console.WriteLine(client.DownloadString("http://www.bom.gov.au/vic/forecasts/ballarat.shtml"));
}
TimTIM Wong
  • 788
  • 5
  • 16