Home > Software engineering >  How do I fix the Problem Error 404 from Webclient
How do I fix the Problem Error 404 from Webclient

Time:02-04

Hey guys,

I have a problem with my code. Since about a week my code is not working anymore without any changes. I am pretty sure, that my could should work. All I get is Error 404: forbidden. Below is a snippet of my Code. I also read about adding a header of the webclient, which did not help. Any other suggestions? I am sorry if my syntax is not that good, it is my first post on stackoverflow.
Thanks in advance!
            string epicId = "ManuelNotManni";
            WebClient webClient = new WebClient();
            Uri uri = new Uri("https://api.tracker.gg/api/v2/rocket-league/standard/profile/epic/");
            string result = String.Empty;
            try
            {
                string website = $"{uri.ToString()}{epicId}?";
                result = webClient.DownloadString(website);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error:\n{ex}");
                Console.ReadLine();
            }
            finally
            {
                webClient.Dispose();
            }

This is the exact error:

System.Net.WebException: The remote server returned an error: (403) Forbidden. at System.Net.HttpWebRequest.GetResponse() at System.Net.WebClient.GetWebResponse(WebRequest request) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream) at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address) at System.Net.WebClient.DownloadString(String address) at TestProject.Program.Main(String[] args) in > C:\Users\Manue\source\repos\TestProject\Program.cs:line 17

CodePudding user response:

You're right. Your code should work fine. Issue is that URL you're requesting which is actually:

https://api.tracker.gg/api/v2/rocket-league/standard/profile/epic/ManuelNotManni?

This returns a 403 status code in any case - no matter if you use a browser, your code or for example postman. I suggest to have a look at the response body while using postman.

It shows this

<html  lang="en-US">
<!--<![endif]-->

<head>

    <title>Attention Required! | Cloudflare</title>

    <meta name="captcha-bypass" id="captcha-bypass" />

CodePudding user response:

Tracker.gg wants API users to register their apps with them before they're given access to the API.

What you need to do is to first head to their Getting Started page. Here you will have to create an app, which should give you an authentication key.

When you have done this, you want to change your code slightly to add the Authentication Header. Like so for example:

        var webClient = new WebClient();
        webclient.Headers.Add("TRN-Api-Key", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")

As a sidenote, WebClient has been deprecated and it's recommended to use HttpClient from now on. Here's your code with HttpClient instead:

var epicId = "ManuelNotManni";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("TRN-Api-Key", "YOUR API KEY GOES HERE");
// Simplifying Uri creation:
var uri = new Uri($"https://api.tracker.gg/api/v2/rocket-league/standard/profile/epic/{epicId}");
var result = string.Empty; // C# prefers lowercase string
try
{
    var response = await httpClient.GetAsync(uri);
    if (response.IsSuccessStatusCode)
    {
        result = await response.Content.ReadAsStringAsync();
    }
    else
    {
        Console.WriteLine($"Unable to retrieve data for {epicId}.");
        Console.WriteLine($"Statuscode: {response.StatusCode}");
        Console.WriteLine($"Reason: {response.ReasonPhrase}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error:\n{ex}");
    Console.ReadLine();
}
finally
{
    httpClient.Dispose();
}
  •  Tags:  
  • Related