如何阅读HTTP响应主体?

时间:2012-02-25 12:56:19

标签: c# asp.net http

我需要使用以下text / plain正文读取HTTP Response 200 OK。 (任何一个)

OK

NUMBER NOT IN LIST

ERROR

但我只知道如何阅读HTTP响应而不是响应主体。对例子的解释将非常感激

2 个答案:

答案 0 :(得分:3)

您可以使用WebClient.DownloadString Method发出HTTP GET请求,并在返回字符串时获取HTTP响应正文:

using (var client = new WebClient())
{
    string result = client.DownloadString("http://example.com/path/to/file");

    switch (result)
    {
        case "OK":
        case "NUMBER NOT IN LIST":
        case "ERROR":
            break;
    }
}

答案 1 :(得分:0)

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://example.com/path/to/file");

        request.UseDefaultCredentials = true;

        if (request.Proxy == null)
        {
            request.Proxy = new WebProxy("http://example.com");
        }
        request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 

HttpWebResponse response =(HttpWebResponse)(request.GetResponse());