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))
dot net
  • 253
  • 5
  • 10

4 Answers4

2

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;
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
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.

Carlos Rodriguez
  • 2,190
  • 2
  • 18
  • 29
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)

AD.Net
  • 13,352
  • 2
  • 28
  • 47
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.

Thomas De Wilde
  • 321
  • 1
  • 8