@@ -25,6 +25,7 @@ public class MTConnectHttpClientStream
2525 private const byte CarriageReturn = 13 ;
2626 private const byte Dash = 45 ;
2727 private static readonly HttpClient _httpClient ;
28+ private static readonly byte [ ] _trimBytes = new byte [ ] { 10 , 13 } ;
2829
2930 private CancellationTokenSource _stop ;
3031 private string _documentFormat = DocumentFormat . XML ;
@@ -152,12 +153,13 @@ public async Task Run(CancellationToken cancellationToken)
152153 }
153154 }
154155
155- // Get the HTTP Stream
156- #if NET5_0_OR_GREATER
157- using ( var stream = await _httpClient . GetStreamAsync ( Url , stop . Token ) )
158- #else
159- using ( var stream = await _httpClient . GetStreamAsync ( Url ) )
160- #endif
156+
157+ var httpRequest = new HttpRequestMessage ( ) ;
158+ httpRequest . RequestUri = new Uri ( Url ) ;
159+ httpRequest . Method = HttpMethod . Get ;
160+
161+ using ( var response = await _httpClient . SendAsync ( httpRequest , HttpCompletionOption . ResponseHeadersRead , stop . Token ) )
162+ using ( var stream = response . Content . ReadAsStream ( ) )
161163 {
162164 var header = new List < byte > ( ) ;
163165 var headerActive = false ;
@@ -169,7 +171,6 @@ public async Task Run(CancellationToken cancellationToken)
169171 var line = new List < byte > ( ) ;
170172 var contentLength = 0 ;
171173 string contentEncoding = null ;
172- var trimBytes = new byte [ ] { 10 , 13 } ;
173174 string lineStr = null ;
174175
175176
@@ -197,9 +198,6 @@ public async Task Run(CancellationToken cancellationToken)
197198
198199 if ( cr && lf )
199200 {
200- // Trim CR and LF bytes from beginning and end
201- //var lineBytes = ObjectExtensions.TrimBytes(line.ToArray(), trimBytes);
202-
203201 // Get the current line as a UTF-8 string
204202 lineStr = Encoding . UTF8 . GetString ( line . ToArray ( ) ) ;
205203
@@ -246,7 +244,7 @@ public async Task Run(CancellationToken cancellationToken)
246244
247245 // Add byte to Line Buffer
248246 line . Add ( ( byte ) b ) ;
249-
247+
250248 prevByte = b ;
251249
252250 // Read the next Byte
@@ -307,16 +305,12 @@ private static Stream ReadBody(Stream stream, int length)
307305 {
308306 var i = 0 ;
309307 var size = length ;
308+ var isHeader = true ;
310309
311310 // Create a buffer to contain body of the response
312311 // based on the size of the content-length received in the Http Headers
313312 var body = new MemoryStream ( size ) ;
314313
315- // Read New Line characters
316- // These always appear after the Http Header
317- stream . ReadByte ( ) ; // 13
318- stream . ReadByte ( ) ; // 10
319-
320314 while ( i < size )
321315 {
322316 // Create a 512 byte buffer
@@ -325,15 +319,20 @@ private static Stream ReadBody(Stream stream, int length)
325319 // Read from the Network stream and store in the chunk buffer
326320 var j = stream . Read ( chunk , 0 , chunk . Length ) ;
327321
322+ // Remove blank lines before header (can cause XML deserialization error if Xml Declaration is not the first line)
323+ if ( isHeader ) chunk = ObjectExtensions . TrimStartBytes ( chunk , _trimBytes ) ;
324+
328325 // Verify bytes read doesn't exceed destination array
329326 // (could be blank lines after document that gets read)
330327 if ( j > size - i ) j = size - i ;
331328
332329 // Add the chunk bytes to the body buffer
333- body . Write ( chunk , 0 , j ) ;
330+ body . Write ( chunk , 0 , Math . Min ( j , chunk . Length ) ) ;
334331
335332 // Increment the index of the body buffer based on the number of bytes read in this chunk
336333 i += j ;
334+
335+ isHeader = false ;
337336 }
338337
339338 return body ;
0 commit comments