web analytics

[Solved] The remote server returned an error: (403) Forbidden.

Options

codeling 1595 - 6639
@2016-02-28 23:38:17

Try to download an image using follow code:

using (WebClient c = new WebClient())

{

      byte[] responseData = c.DownloadData(imageFileUrl);

      //.....

}

And get the following exception:

The remote server returned an error: (403) Forbidden.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadData(Uri address)
@2016-02-28 23:43:44

Some web servers can block requests based on the user agent string they provide. Try to add the user agent of a browser, any browser would be fine.

using (WebClient c = new WebClient())

{
      c.Headers.Add("User-Agent: Other");

      byte[] responseData = c.DownloadData(imageFileUrl);

      //.....

}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com