Search code examples
c#http-status-code-404discordwebclientdownloadstring

c# - DownloadString with WebClient - 404 Error


I am trying to download string from discord webhook :("https://discordapp.com/api/webhooks/704770710509453432/GdR4absQHKDKjUiNMpw3aIpX2tx-9Z7nmPE2Sn3TUkQDM12zUczaV-m80orh7WGVzvGK")

When I normally open the site with browser string is : {"message": "Unknown Webhook", "code": 10015}

But when I do that with WebClient:

           WebClient wc = new WebClient();
           string site = "https://discordapp.com/api/webhooks/704770710509453432/GdR4absQHKDKjUiNMpw3aIpX2tx-9Z7nmPE2Sn3TUkQDM12zUczaV-m80orh7WGVzvGK";
           string x = wc.DownloadString(site);

It gives an "404 Error". Is there a way to get that {"message": "Unknown Webhook", "code": 10015} string with c#?


Solution

  • A rough guess would be that it's to do with the accept header. Check the documentation for the api, but my browser sent an additional 17 headers along with the request.


    The simple answer would be, use HttpClient. It's recommended for new development and also gives the correct response here:

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    namespace discordapi_headers
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                var client = new HttpClient();
                var response = await client.GetAsync("https://discordapp.com/api/webhooks/704770710509453432/GdR4absQHKDKjUiNMpw3aIpX2tx-9Z7nmPE2Sn3TUkQDM12zUczaV-m80orh7WGVzvGK");
                Console.WriteLine(await response.Content.ReadAsStringAsync());
            }
        }
    }
    

    However that doesn't help if you can't. Also I'm intrigued now...


    Ha! I should have listened to my own advice. For web service related stuff, you can't beat fiddler and postman. It turns out that the api is returning a custom 404 to the browser that has json content.

    Unfortunately DownloadString sees the 404 and throws a WebException.

    Here's an updated version that works, using HttpClient and WebClient.

    using System;
    using System.Net;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace discordapi_headers
    {
        class Program
        {
            static readonly string url = "https://discordapp.com/api/webhooks/704770710509453432/GdR4absQHKDKjUiNMpw3aIpX2tx-9Z7nmPE2Sn3TUkQDM12zUczaV-m80orh7WGVzvGK";
    
            static async Task Main(string[] args)
            {
                Console.WriteLine(await UseHttpClient(url));
                Console.WriteLine(await UseWebClient(url));
            }
    
            private static async Task<string> UseHttpClient(string url)
            {
                var client = new HttpClient();
                var response = await client.GetAsync(url);
                return await response.Content.ReadAsStringAsync();
            }
    
            private static async Task<string> UseWebClient(string url)
            {
                var client = new WebClient();
                try
                {
                    var response = await client.DownloadStringTaskAsync(url);
                    return response;
                }
                catch (WebException wex)
                {
                    using (var s = wex.Response.GetResponseStream())
                    {
                        var buffer = new byte[wex.Response.ContentLength];
                        var contentBytes = await s.ReadAsync(buffer, 0, buffer.Length);
                        var content = Encoding.UTF8.GetString(buffer);
                        return content;
                    }
                }
            }
        }
    }