从Webclient获取html,即使Webclient抛出异常

本文关键字:Webclient 即使 抛出异常 html 获取 | 更新日期: 2023-09-27 18:03:42

我正在针对web API工作,然后我向web API进行身份验证,并提供错误的用户名或密码,该网站返回"远程服务器返回错误:(401)未经授权。"这很好。但是该网站也返回JSON的详细信息,但我不知道如何访问这些信息,然后我得到一个401异常。

有人知道吗?下面是我使用的代码:

private string Post(string data, string URI)
    {
        string response = string.Empty;
        using (var wc = new WebClient())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            try
            {
                response = wc.UploadString(URI, data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        return response;
    }

谢谢

从Webclient获取html,即使Webclient抛出异常

你应该收到一个webeexception。
WebReception有一个

反应

属性,它应该包含您正在查找的内容。您也可以检查所有其他属性