@@ -549,23 +549,19 @@ public static string SendStringToUrl(this string url, string method = null,
549
549
550
550
if ( requestBody != null )
551
551
{
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 ) ;
557
555
}
558
556
else if ( method != null && HasRequestBody ( method ) )
559
557
{
560
558
webReq . ContentLength = 0 ;
561
559
}
562
560
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 ) ;
569
565
}
570
566
571
567
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
591
587
592
588
if ( requestBody != null )
593
589
{
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 ) ;
599
593
}
600
594
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 ( ) ;
609
599
}
610
600
611
601
public static byte [ ] GetBytesFromUrl ( this string url , string accept = "*/*" ,
@@ -950,22 +940,24 @@ public static string GetResponseBody(this Exception ex)
950
940
951
941
public static string ReadToEnd ( this WebResponse webRes )
952
942
{
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 ) ;
957
951
}
958
952
959
953
public static IEnumerable < string > ReadLines ( this WebResponse webRes )
960
954
{
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 )
963
959
{
964
- string line ;
965
- while ( ( line = reader . ReadLine ( ) ) != null )
966
- {
967
- yield return line ;
968
- }
960
+ yield return line ;
969
961
}
970
962
}
971
963
@@ -974,11 +966,24 @@ public static HttpWebResponse GetErrorResponse(this string url)
974
966
try
975
967
{
976
968
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 ;
982
987
}
983
988
catch ( WebException webEx )
984
989
{
@@ -1091,16 +1096,11 @@ public static void UploadFile(this WebRequest webRequest, Stream fileStream, str
1091
1096
return ;
1092
1097
}
1093
1098
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 ) ;
1104
1104
}
1105
1105
1106
1106
public static void UploadFile ( this WebRequest webRequest , Stream fileStream , string fileName )
@@ -1269,7 +1269,7 @@ public static string GetExtension(string mimeType)
1269
1269
throw new NotSupportedException ( "Unknown mimeType: " + mimeType ) ;
1270
1270
}
1271
1271
1272
- //lowercases and trims left part of content-type prior ';'
1272
+ //Lower cases and trims left part of content-type prior ';'
1273
1273
public static string GetRealContentType ( string contentType )
1274
1274
{
1275
1275
if ( contentType == null )
0 commit comments