如何读取HTTP响应正文
本文关键字:HTTP 响应 正文 读取 何读取 | 更新日期: 2023-09-27 18:25:51
我需要阅读HTTP响应200 OK,其中包含以下文本/纯文本。(任意一个)
OK
NUMBER NOT IN LIST
ERROR
但我只知道如何读取HTTP响应,而不知道响应体。举例说明将不胜感激
您可以使用WebClient.DownloadString方法发出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;
}
}
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响应=(HttpWebResponse)(request.GetResponse());