0

I have the following string in which I am trying to get the StatusCode: value,output should be 409..I tried as follows buts not working,whats the right way to do this?

string output = 'QLASR: Bad Response Conflict StatusCode: 409, ReasonPhrase: 'Improper Software Product Build Name already present in the database', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept Cache-Control: no-cache Date: Tue, 11 Jul 2017 22:18:26 GMT Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 68 Content-Type: text/plain; charset=utf-8 Expires: -1 }' output.Any(x => x.Equals(StatusCode)) 
7
  • Will the word always be StatusCode: and will a comma always follow the value? Commented Jul 11, 2017 at 22:34
  • @maccettura - Yes,your assumption is correct Commented Jul 11, 2017 at 22:35
  • Also, this looks like poorly formed JSON, is it supposed to be JSON? Commented Jul 11, 2017 at 22:36
  • If its JSON, then just deserialize to an object and grab the value there. Commented Jul 11, 2017 at 22:38
  • There's varying degrees of robustness with which you could implement this, depending on what assumptions you can make about the shape of the text. If your input isn't already some standard parseable format, and it's too much work to write a proper parser, the go-to approach is usually to use a regex. Commented Jul 11, 2017 at 22:38

4 Answers 4

3

You shouldn't have this string to begin with. It's an improperly read HttpResponseMessage. There's some code issuing an HTTP request somewhere, and from there you can just call access the response's StatusCode property:

httpClient = new HttpClient(); var response = await httpClient.GetAsync("..."); var statusCode = response.StatusCode; 
Sign up to request clarification or add additional context in comments.

Comments

0
const string statusCodeIdentifier = "StatusCode:"; var outputWithoutSpaces = output.Replace(" ", ""); var statusCodeIndex = outputWithoutSpaces.IndexOf(statusCodeIdentifier); var parsedCode = outputWithoutSpaces.Substring(statusCodeIndex + statusCodeIdentifier.Length, 3); 

Not the prettiest, and this assumes that status codes are always 3 digits. But assuming that your strings are always of this format, it should work.

You had mentioned in the comments the output is supposed to be JSON. It didn't look like that was the case in your example string though. If it is actually JSON, then I would instead used Newtonsoft.JSON to just deserialize the object and handle it that way. It'd be much more manageable.

Comments

0

You can find the index of StatusCode:, use that index and find the substring from that index to the index of , (you will have to use the length of StatusCode: to get the value)

Comments

0

A regular expression seems to be the best option here.

int statusCode = 0; string output = 'QLASR: Bad Response Conflict StatusCode: 409, ...' string pattern = @"StatusCode\:\s?(\d+)"; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); Match m = r.Match(output); if (m.Success) { statusCode = (int)m.Groups[1].Value; Debug.WriteLine(statusCode); } 

This regular expression looks for 'StatusCode' followed by a colon, followed by a whitespace (or not), followed by multiple digits. Parenthesis allow you to group things.

So, when this regular expression is executed it matches your output and finds 'StatusCode: 409'. We then use the group 1, which contains the value within the parenthesis, to get the status code you wanted. (Group 0 = the entire match; So that would be 'StatusCode: 409')

But tbh, your 'output' looks like the response code from the HttpClient. And I'm sure the response has a StatusCode property publicly accessible.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.