@@ -549,23 +549,19 @@ public static string SendStringToUrl(this string url, string method = null,
549549
550550 if ( requestBody != null )
551551 {
552- using ( var reqStream = PclExport . Instance . GetRequestStream ( webReq ) )
553- using ( var writer = new StreamWriter ( reqStream , UseEncoding ) )
554- {
555- writer . Write ( requestBody ) ;
556- }
552+ using var reqStream = PclExport . Instance . GetRequestStream ( webReq ) ;
553+ using var writer = new StreamWriter ( reqStream , UseEncoding ) ;
554+ writer . Write ( requestBody ) ;
557555 }
558556 else if ( method != null && HasRequestBody ( method ) )
559557 {
560558 webReq . ContentLength = 0 ;
561559 }
562560
563- using ( var webRes = PclExport . Instance . GetResponse ( webReq ) )
564- using ( var stream = webRes . GetResponseStream ( ) )
565- {
566- responseFilter ? . Invoke ( ( HttpWebResponse ) webRes ) ;
567- return stream . ReadToEnd ( UseEncoding ) ;
568- }
561+ using var webRes = webReq . GetResponse ( ) ;
562+ using var stream = webRes . GetResponseStream ( ) ;
563+ responseFilter ? . Invoke ( ( HttpWebResponse ) webRes ) ;
564+ return stream . ReadToEnd ( UseEncoding ) ;
569565 }
570566
571567 public static async Task < string > SendStringToUrlAsync ( this string url , string method = null , string requestBody = null ,
@@ -591,21 +587,15 @@ public static async Task<string> SendStringToUrlAsync(this string url, string me
591587
592588 if ( requestBody != null )
593589 {
594- using ( var reqStream = PclExport . Instance . GetRequestStream ( webReq ) )
595- using ( var writer = new StreamWriter ( reqStream , UseEncoding ) )
596- {
597- writer . Write ( requestBody ) ;
598- }
590+ using var reqStream = PclExport . Instance . GetRequestStream ( webReq ) ;
591+ using var writer = new StreamWriter ( reqStream , UseEncoding ) ;
592+ await writer . WriteAsync ( requestBody ) ;
599593 }
600594
601- using ( var webRes = await webReq . GetResponseAsync ( ) . ConfigAwait ( ) )
602- {
603- responseFilter ? . Invoke ( ( HttpWebResponse ) webRes ) ;
604- using ( var stream = webRes . GetResponseStream ( ) )
605- {
606- return await stream . ReadToEndAsync ( ) . ConfigAwait ( ) ;
607- }
608- }
595+ using var webRes = await webReq . GetResponseAsync ( ) . ConfigAwait ( ) ;
596+ responseFilter ? . Invoke ( ( HttpWebResponse ) webRes ) ;
597+ using var stream = webRes . GetResponseStream ( ) ;
598+ return await stream . ReadToEndAsync ( ) . ConfigAwait ( ) ;
609599 }
610600
611601 public static byte [ ] GetBytesFromUrl ( this string url , string accept = "*/*" ,
@@ -950,22 +940,24 @@ public static string GetResponseBody(this Exception ex)
950940
951941 public static string ReadToEnd ( this WebResponse webRes )
952942 {
953- using ( var stream = webRes . GetResponseStream ( ) )
954- {
955- return stream . ReadToEnd ( UseEncoding ) ;
956- }
943+ using var stream = webRes . GetResponseStream ( ) ;
944+ return stream . ReadToEnd ( UseEncoding ) ;
945+ }
946+
947+ public static Task < string > ReadToEndAsync ( this WebResponse webRes )
948+ {
949+ using var stream = webRes . GetResponseStream ( ) ;
950+ return stream . ReadToEndAsync ( UseEncoding ) ;
957951 }
958952
959953 public static IEnumerable < string > ReadLines ( this WebResponse webRes )
960954 {
961- using ( var stream = webRes . GetResponseStream ( ) )
962- using ( var reader = new StreamReader ( stream , UseEncoding , true , 1024 , leaveOpen : true ) )
955+ using var stream = webRes . GetResponseStream ( ) ;
956+ using var reader = new StreamReader ( stream , UseEncoding , true , 1024 , leaveOpen : true ) ;
957+ string line ;
958+ while ( ( line = reader . ReadLine ( ) ) != null )
963959 {
964- string line ;
965- while ( ( line = reader . ReadLine ( ) ) != null )
966- {
967- yield return line ;
968- }
960+ yield return line ;
969961 }
970962 }
971963
@@ -974,11 +966,24 @@ public static HttpWebResponse GetErrorResponse(this string url)
974966 try
975967 {
976968 var webReq = WebRequest . Create ( url ) ;
977- using ( var webRes = PclExport . Instance . GetResponse ( webReq ) )
978- {
979- webRes . ReadToEnd ( ) ;
980- return null ;
981- }
969+ using var webRes = PclExport . Instance . GetResponse ( webReq ) ;
970+ webRes . ReadToEnd ( ) ;
971+ return null ;
972+ }
973+ catch ( WebException webEx )
974+ {
975+ return ( HttpWebResponse ) webEx . Response ;
976+ }
977+ }
978+
979+ public static async Task < HttpWebResponse > GetErrorResponseAsync ( this string url )
980+ {
981+ try
982+ {
983+ var webReq = WebRequest . Create ( url ) ;
984+ using var webRes = await webReq . GetResponseAsync ( ) ;
985+ await webRes . ReadToEndAsync ( ) ;
986+ return null ;
982987 }
983988 catch ( WebException webEx )
984989 {
@@ -1091,16 +1096,11 @@ public static void UploadFile(this WebRequest webRequest, Stream fileStream, str
10911096 return ;
10921097 }
10931098
1094- using ( var outputStream = PclExport . Instance . GetRequestStream ( httpReq ) )
1095- {
1096- outputStream . Write ( headerBytes , 0 , headerBytes . Length ) ;
1097-
1098- fileStream . CopyTo ( outputStream , 4096 ) ;
1099-
1100- outputStream . Write ( boundaryBytes , 0 , boundaryBytes . Length ) ;
1101-
1102- PclExport . Instance . CloseStream ( outputStream ) ;
1103- }
1099+ using var outputStream = PclExport . Instance . GetRequestStream ( httpReq ) ;
1100+ outputStream . Write ( headerBytes , 0 , headerBytes . Length ) ;
1101+ fileStream . CopyTo ( outputStream , 4096 ) ;
1102+ outputStream . Write ( boundaryBytes , 0 , boundaryBytes . Length ) ;
1103+ PclExport . Instance . CloseStream ( outputStream ) ;
11041104 }
11051105
11061106 public static void UploadFile ( this WebRequest webRequest , Stream fileStream , string fileName )
@@ -1269,7 +1269,7 @@ public static string GetExtension(string mimeType)
12691269 throw new NotSupportedException ( "Unknown mimeType: " + mimeType ) ;
12701270 }
12711271
1272- //lowercases and trims left part of content-type prior ';'
1272+ //Lower cases and trims left part of content-type prior ';'
12731273 public static string GetRealContentType ( string contentType )
12741274 {
12751275 if ( contentType == null )
0 commit comments