从Webclient获取HTML,即使Webclient也抛出异常

时间:2013-03-12 12:50:36

标签: c# webclient http-status-code-401

我正在使用Web API,然后我对webb 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;
    }

由于

1 个答案:

答案 0 :(得分:2)

您应该收到WebException WebReception有一个

  

响应

属性,应包含您要查找的内容。您也可以查看所有其他房产。