3

Is there a simple way to parse an HTTP Response string such as follows:

"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"

I would like to get the Status Code. I do not necessarily need to turn this in to an HttpResponse object, but that would be acceptable as well as just parsing out the Status Code. Would I be able to parse that into the HttpStatusCode enum?

I am using a sockets based approach and cannot change the way I am getting my response. I will only have this string to work with.

2
  • This has been asked here already, take a look here: stackoverflow.com/questions/2808866/… Commented Jul 21, 2011 at 16:20
  • No, that's a different question. Commented Nov 8, 2013 at 16:26

6 Answers 6

5

EDIT Taking into account "I am using a sockets based approach and cannot change the way I am getting my response. I will only have this string to work with".

How about

  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]);

I had originally suggested this:

  HttpWebRequest webRequest =(HttpWebRequest)WebRequest.Create("http://www.gooogle.com/");
  webRequest.AllowAutoRedirect = false;
  HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
  int statuscode = (int)response.StatusCode)
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure how this is supposed to function correctly, as each name/value pair are separated by \r\n, not " ".... You'd want to first split by \r\n then split each of those by space in order to get true name/values...
@tecknik Because he is trying to get the response code which appears after the first string. If you wanted to split the whole thing up into key value pairs you could do that, but he just wanted the get the code out, in this case "200".
@TechnikEmpire first line is not structured like hash table in http header. Although subsequent lines are structured and can be split at ":" (colon) to get key/value pair for further parsing of headers.
@devprashant My point is, having written a fully compliant HTTP/S proxy, that this is a very poor way to parse the headers. In fact this doesn't parse the headers at all, it'll split them a way that will expose the status code string, but absolutely break everything else.
4

HTTP is a pretty simple protocol, the following should get the status code out pretty reliably (updated to be a tad more robust):

int statusCodeStart = httpString.IndexOf(' ') + 1;
int statusCodeEnd = httpString.IndexOf(' ', statusCodeStart);

return httpString.Substring(statusCodeStart, statusCodeEnd - statusCodeStart);

If you really wanted to you could add a sanity check to make sure that the string starts with "HTTP", but then if you wanted robustness you could also just implement a HTTP parser.

To be honest this would probably do! :-)

httpString.Substring(9, 3);

Comments

2

If it's just a string could you not just use a regex to extract the status code?

1 Comment

Agreed, according the the OP " I will only have this string to work with." ... so work with the string....
1

Either do what DD59 suggests or use a regular expression.

Comments

1

This updates the marked answer to handle some corner cases:

    static HttpStatusCode? GetStatusCode(string response)
    {
        string rawCode = response.Split(' ').Skip(1).FirstOrDefault();

        if (!string.IsNullOrWhiteSpace(rawCode) && rawCode.Length > 2)
        {
            rawCode = rawCode.Substring(0, 3);

            int code;

            if (int.TryParse(rawCode, out code))
            {
                return (HttpStatusCode)code;
            }
        }

        return null;
    }

Comments

0

coz the format of the status code remains same u can probably use smthing like this.

var responseArray = Regex.Split(response, "\r\n");
 if(responseArray.Length)>0
 {
 var statusCode = (int)responseArray[0].split(' ')[1];
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.