在C#中解析原始HTTP响应以获取状态代码

Ian*_*ien 3 c# sockets http httpresponse

是否有一种简单的方法来解析HTTP响应字符串,如下所示:

"HTTP/1.1 200 OK\r\nContent-Length: 1433\r\nContent-Type: text/html\r\nContent-Location: http://server/iisstart.htm\r\nLast-Modified: Fri, 21 Feb 2003 23:48:30 GMT\r\nAccept-Ranges: bytes\r\nETag: \"09b60bc3dac21:1ca9\"\r\nServer: Microsoft-IIS/6.0\r\nX-Po"
Run Code Online (Sandbox Code Playgroud)

我想获得状态代码.我不一定需要将其转换为HttpResponse对象,但这只是解析状态代码是可以接受的.我可以将其解析为HttpStatusCode枚举吗?

我正在使用基于套接字的方法,并且无法改变我获得响应的方式.我只会使用此字符串.

ins*_*ite 5

编辑考虑到" 我正在使用基于套接字的方法,并且无法改变我获得响应的方式.我只会使用此字符串 ".

怎么样

  string response = "HTTP/1.1 200 OK\r\nContent-Length: 1433\r\nContent-Type: text/html\r\nContent-Location: http://server/iisstart.htm\r\nLast-Modified: Fri, 21 Feb 2003 23:48:30 GMT\r\nAccept-Ranges: bytes\r\nETag: \"09b60bc3dac21:1ca9\"\r\nServer: Microsoft-IIS/6.0\r\nX-Po";

  string code = response.Split(' ')[1];
  // int code = int.Parse(response.Split(' ')[1]);
Run Code Online (Sandbox Code Playgroud)

我最初建议这个:

  HttpWebRequest webRequest =(HttpWebRequest)WebRequest.Create("http://www.gooogle.com/");
  webRequest.AllowAutoRedirect = false;
  HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
  int statuscode = (int)response.StatusCode)
Run Code Online (Sandbox Code Playgroud)