@@ -75,7 +75,7 @@ private string GetQueryUrl(string address)
7575 first = AppendParameter ( parameters , address , Query , first ) ;
7676 first = AppendGlobalParameters ( parameters , first ) ;
7777
78- return String . Format ( FormattedQuery , parameters . ToString ( ) , _bingKey ) ;
78+ return String . Format ( FormattedQuery , parameters , _bingKey ) ;
7979 }
8080
8181 private string GetQueryUrl ( string street , string city , string state , string postalCode , string country )
@@ -89,7 +89,7 @@ private string GetQueryUrl(string street, string city, string state, string post
8989 first = AppendParameter ( parameters , street , Address , first ) ;
9090 first = AppendGlobalParameters ( parameters , first ) ;
9191
92- return String . Format ( FormattedQuery , parameters . ToString ( ) , _bingKey ) ;
92+ return String . Format ( FormattedQuery , parameters , _bingKey ) ;
9393 }
9494
9595 private string GetQueryUrl ( double latitude , double longitude )
@@ -249,15 +249,11 @@ protected virtual IEnumerable<BingAddress> ParseResponse(Json.Response response)
249249 if ( resourceSet is null )
250250 continue ;
251251
252- var locations = resourceSet . Locations ;
253- if ( locations . IsNullOrEmpty ( ) )
254- continue ;
255-
256- foreach ( var location in locations . Where ( location => location ? . Point ? . Coordinates is { Length : >= 2 }
252+ foreach ( var location in resourceSet . Resources . OfType < Json . Location > ( ) . Where ( location => location . Point ? . Coordinates is { Length : >= 2 }
257253 && location . Address is not null
258254 && ! String . IsNullOrWhiteSpace ( location . Address . FormattedAddress ) ) )
259255 {
260- var coordinates = location ! . Point ! . Coordinates ! ;
256+ var coordinates = location . Point ! . Coordinates ! ;
261257
262258 if ( ! Enum . TryParse ( location . EntityType , out EntityType entityType ) )
263259 entityType = EntityType . Unknown ;
@@ -309,8 +305,8 @@ protected virtual HttpClient BuildClient()
309305
310306 if ( ! response . IsSuccessStatusCode )
311307 {
312- var body = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
313- throw new BingGeocodingException ( new HttpRequestException ( $ "Bing Maps request failed ({ ( int ) response . StatusCode } { response . ReasonPhrase } ).{ BuildResponsePreview ( body ) } ") ) ;
308+ var preview = await BuildResponsePreviewAsync ( response . Content ) . ConfigureAwait ( false ) ;
309+ throw new BingGeocodingException ( new HttpRequestException ( $ "Bing Maps request failed ({ ( int ) response . StatusCode } { response . ReasonPhrase } ).{ preview } ") ) ;
314310 }
315311
316312 using ( var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) )
@@ -335,16 +331,21 @@ private ConfidenceLevel EvaluateConfidence(string? confidence)
335331 return ConfidenceLevel . Unknown ;
336332 }
337333
338- private static string BuildResponsePreview ( string ? body )
334+ private static async Task < string > BuildResponsePreviewAsync ( HttpContent content )
339335 {
340- if ( String . IsNullOrWhiteSpace ( body ) )
336+ using var stream = await content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
337+ using var reader = new StreamReader ( stream , Encoding . UTF8 , detectEncodingFromByteOrderMarks : true , bufferSize : 1024 , leaveOpen : false ) ;
338+ var buffer = new char [ 256 ] ;
339+ int read = await reader . ReadBlockAsync ( buffer , 0 , buffer . Length ) . ConfigureAwait ( false ) ;
340+
341+ if ( read == 0 )
341342 return String . Empty ;
342343
343- var preview = body ! . Trim ( ) ;
344- if ( preview . Length > 256 )
345- preview = preview . Substring ( 0 , 256 ) + "..." ;
344+ var preview = new string ( buffer , 0 , read ) . Trim ( ) ;
345+ if ( String . IsNullOrWhiteSpace ( preview ) )
346+ return String . Empty ;
346347
347- return " Response preview: " + preview ;
348+ return " Response preview: " + preview + ( reader . EndOfStream ? String . Empty : "..." ) ;
348349 }
349350
350351 private string BingUrlEncode ( string toEncode )
0 commit comments