private async Task <EndpointStatus> GetStatus(Stream stream) { var request = string.Format("GET {2} HTTP/1.1{0}Host: {1}{0}Connection: Close{0}{0}", "\r\n", _server, _path); var sendBuffer = Encoding.ASCII.GetBytes(request); var receiveBuffer = new Byte[256]; await stream.WriteAsync(sendBuffer, 0, sendBuffer.Length); int receiveBufferLength; var rawResponse = new StringBuilder(); EndpointStatus endpointStatus = null; do { receiveBufferLength = await stream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length); rawResponse.Append(Encoding.ASCII.GetString(receiveBuffer, 0, receiveBufferLength)); var statusLineMatch = _statusLineRegex.Match(rawResponse.ToString()); if (statusLineMatch.Success) { // As soon as we have the status line, we're done. var statusCode = (HttpStatusCode)int.Parse(statusLineMatch.Groups[_statusCode].Value); endpointStatus = new EndpointStatus(true, _endpoint, statusCode); break; } } while (receiveBufferLength > 0); return(endpointStatus ?? new EndpointStatus(false, _endpoint)); }
private async Task<EndpointStatus> GetStatus(Stream stream) { var request = string.Format("GET {2} HTTP/1.1{0}Host: {1}{0}Connection: Close{0}{0}", "\r\n", _server, _path); var sendBuffer = Encoding.ASCII.GetBytes(request); var receiveBuffer = new Byte[256]; await stream.WriteAsync(sendBuffer, 0, sendBuffer.Length).ConfigureAwait(false); int receiveBufferLength; var rawResponse = new StringBuilder(); EndpointStatus endpointStatus = null; do { receiveBufferLength = await stream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length).ConfigureAwait(false); rawResponse.Append(Encoding.ASCII.GetString(receiveBuffer, 0, receiveBufferLength)); var statusLineMatch = _statusLineRegex.Match(rawResponse.ToString()); if (statusLineMatch.Success) { // As soon as we have the status line, we're done. var statusCode = (HttpStatusCode)int.Parse(statusLineMatch.Groups[_statusCode].Value); endpointStatus = new EndpointStatus(true, _endpoint, statusCode); break; } } while (receiveBufferLength > 0); return endpointStatus ?? new EndpointStatus(false, _endpoint); }