1+ #if ! NET6_0_OR_GREATER
12using System ;
23using System . Collections . Generic ;
34using System . IO ;
@@ -773,54 +774,6 @@ public static async Task<Stream> SendStreamToUrlAsync(this string url, string me
773774 return stream ;
774775 }
775776
776- public static bool IsAny300 ( this Exception ex )
777- {
778- var status = ex . GetStatus ( ) ;
779- return status >= HttpStatusCode . MultipleChoices && status < HttpStatusCode . BadRequest ;
780- }
781-
782- public static bool IsAny400 ( this Exception ex )
783- {
784- var status = ex . GetStatus ( ) ;
785- return status >= HttpStatusCode . BadRequest && status < HttpStatusCode . InternalServerError ;
786- }
787-
788- public static bool IsAny500 ( this Exception ex )
789- {
790- var status = ex . GetStatus ( ) ;
791- return status >= HttpStatusCode . InternalServerError && ( int ) status < 600 ;
792- }
793-
794- public static bool IsNotModified ( this Exception ex )
795- {
796- return GetStatus ( ex ) == HttpStatusCode . NotModified ;
797- }
798-
799- public static bool IsBadRequest ( this Exception ex )
800- {
801- return GetStatus ( ex ) == HttpStatusCode . BadRequest ;
802- }
803-
804- public static bool IsNotFound ( this Exception ex )
805- {
806- return GetStatus ( ex ) == HttpStatusCode . NotFound ;
807- }
808-
809- public static bool IsUnauthorized ( this Exception ex )
810- {
811- return GetStatus ( ex ) == HttpStatusCode . Unauthorized ;
812- }
813-
814- public static bool IsForbidden ( this Exception ex )
815- {
816- return GetStatus ( ex ) == HttpStatusCode . Forbidden ;
817- }
818-
819- public static bool IsInternalServerError ( this Exception ex )
820- {
821- return GetStatus ( ex ) == HttpStatusCode . InternalServerError ;
822- }
823-
824777 public static HttpStatusCode ? GetResponseStatus ( this string url )
825778 {
826779 try
@@ -838,74 +791,6 @@ public static bool IsInternalServerError(this Exception ex)
838791 }
839792 }
840793
841- public static HttpStatusCode ? GetStatus ( this Exception ex )
842- {
843- if ( ex == null )
844- return null ;
845-
846- if ( ex is WebException webEx )
847- return GetStatus ( webEx ) ;
848-
849- if ( ex is IHasStatusCode hasStatus )
850- return ( HttpStatusCode ) hasStatus . StatusCode ;
851-
852- return null ;
853- }
854-
855- public static HttpStatusCode ? GetStatus ( this WebException webEx )
856- {
857- var httpRes = webEx ? . Response as HttpWebResponse ;
858- return httpRes ? . StatusCode ;
859- }
860-
861- public static bool HasStatus ( this Exception ex , HttpStatusCode statusCode )
862- {
863- return GetStatus ( ex ) == statusCode ;
864- }
865-
866- public static string GetResponseBody ( this Exception ex )
867- {
868- if ( ! ( ex is WebException webEx ) || webEx . Response == null || webEx . Status != WebExceptionStatus . ProtocolError )
869- return null ;
870-
871- var errorResponse = ( HttpWebResponse ) webEx . Response ;
872- using var responseStream = errorResponse . GetResponseStream ( ) ;
873- return responseStream . ReadToEnd ( UseEncoding ) ;
874- }
875-
876- public static async Task < string > GetResponseBodyAsync ( this Exception ex , CancellationToken token = default )
877- {
878- if ( ! ( ex is WebException webEx ) || webEx . Response == null || webEx . Status != WebExceptionStatus . ProtocolError )
879- return null ;
880-
881- var errorResponse = ( HttpWebResponse ) webEx . Response ;
882- using var responseStream = errorResponse . GetResponseStream ( ) ;
883- return await responseStream . ReadToEndAsync ( UseEncoding ) . ConfigAwait ( ) ;
884- }
885-
886- public static string ReadToEnd ( this WebResponse webRes )
887- {
888- using var stream = webRes . GetResponseStream ( ) ;
889- return stream . ReadToEnd ( UseEncoding ) ;
890- }
891-
892- public static Task < string > ReadToEndAsync ( this WebResponse webRes )
893- {
894- using var stream = webRes . GetResponseStream ( ) ;
895- return stream . ReadToEndAsync ( UseEncoding ) ;
896- }
897-
898- public static IEnumerable < string > ReadLines ( this WebResponse webRes )
899- {
900- using var stream = webRes . GetResponseStream ( ) ;
901- using var reader = new StreamReader ( stream , UseEncoding , true , 1024 , leaveOpen : true ) ;
902- string line ;
903- while ( ( line = reader . ReadLine ( ) ) != null )
904- {
905- yield return line ;
906- }
907- }
908-
909794 public static HttpWebResponse GetErrorResponse ( this string url )
910795 {
911796 try
@@ -1187,8 +1072,29 @@ private static byte[] GetHeaderBytes(string fileName, string mimeType, string fi
11871072 var headerBytes = header . ToAsciiBytes ( ) ;
11881073 return headerBytes ;
11891074 }
1190- }
11911075
1076+ public static HttpWebRequest With ( this HttpWebRequest httpReq ,
1077+ string accept = null ,
1078+ string userAgent = null ,
1079+ Dictionary < string , string > headers = null )
1080+ {
1081+ if ( accept != null )
1082+ httpReq . Headers . Add ( HttpHeaders . Accept , accept ) ;
1083+
1084+ if ( userAgent != null )
1085+ httpReq . UserAgent = userAgent ;
1086+
1087+ if ( headers != null )
1088+ {
1089+ foreach ( var entry in headers )
1090+ {
1091+ httpReq . Headers [ entry . Key ] = entry . Value ;
1092+ }
1093+ }
1094+
1095+ return httpReq ;
1096+ }
1097+ }
11921098
11931099public interface IHttpResultsFilter : IDisposable
11941100{
@@ -1241,3 +1147,4 @@ public void UploadStream(HttpWebRequest webRequest, Stream fileStream, string fi
12411147 UploadFileFn ? . Invoke ( webRequest , fileStream , fileName ) ;
12421148 }
12431149}
1150+ #endif
0 commit comments