【问题标题】:c# - DownloadString with WebClient - 404 Errorc# - 使用 WebClient 下载字符串 - 404 错误
【发布时间】:2020-09-18 18:12:29
【问题描述】:

我正在尝试从 discord webhook 下载字符串 :("https://discordapp.com/api/webhooks/704770710509453432/GdR4absQHKDKjUiNMpw3aIpX2tx-9Z7nmPE2Sn3TUkQDM12zUczaV-m80orh7WGVzvGK")

当我正常打开网站时,浏览器字符串是:{"message": "Unknown Webhook", "code": 10015}

但是当我使用 WebClient 执行此操作时:

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

它给出“404 错误”。有没有办法用 c# 获取 {"message": "Unknown Webhook", "code": 10015} 字符串?

【问题讨论】:

  • 404错误是找不到页面。您使用的是 https(安全),因此可能是 SSL/TLS 问题。查找错误的最佳方法是使用像wireshark 或fiddler 这样的嗅探器。比较第一个请求中的标头与无效的标头。 c# 中的默认标头与 webhook 不同。修改 c# 中的标头,使它们看起来与工作中的 webhol 完全一样。
  • 即使使用 WebBrowser,您也会获得404 StatusCode。 WebBrowser 所做的你没有做的是读取[WebException].Response 对象返回的响应流。您可以使用 StreamReader 读取该流。在这种情况下,您需要使用 HttpWebRequest 或 HttpClient。捕获 WebException(无论如何它是强制性的)并使用[StreamReader].ReadToEnd() 读取响应对象流。你会得到你的 json 响应,就像你在 WebBrowser 中看到的那样。

标签: c# http-status-code-404 discord webclient downloadstring


【解决方案1】:

粗略的猜测是它与接受标头有关。检查 api 的文档,但我的浏览器随请求发送了额外的 17 headers


简单的答案是,使用HttpClient。建议用于新开发,并在此处给出正确响应:

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());
        }
    }
}

但是,如果您不能这样做,那也无济于事。我现在也很感兴趣……


哈!我应该听听我自己的建议。对于 Web 服务相关的东西,你不能打败 fiddler 和 postman。事实证明,该 api 正在向具有 json 内容的浏览器返回自定义 404。

不幸的是,DownloadString 看到 404 并引发 WebException。

这是一个使用 HttpClient 和 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;
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多