Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 271bb28

Browse files
committed
extract WebRequest ext methods
1 parent 5f0e936 commit 271bb28

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/ServiceStack.Text/HttpUtils.WebRequest.cs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,21 @@ public static string SendStringToUrl(this string url, string method = null,
455455
Action<HttpWebRequest> requestFilter = null, Action<HttpWebResponse> responseFilter = null)
456456
{
457457
var webReq = (HttpWebRequest)WebRequest.Create(url);
458+
return SendStringToUrl(webReq, method, requestBody, contentType, accept, requestFilter, responseFilter);
459+
}
460+
461+
public static async Task<string> SendStringToUrlAsync(this string url, string method = null,
462+
string requestBody = null,
463+
string contentType = null, string accept = "*/*", Action<HttpWebRequest> requestFilter = null,
464+
Action<HttpWebResponse> responseFilter = null, CancellationToken token = default)
465+
{
466+
var webReq = (HttpWebRequest)WebRequest.Create(url);
467+
return await SendStringToUrlAsync(webReq, method, requestBody, contentType, accept, requestFilter, responseFilter);
468+
}
469+
470+
public static string SendStringToUrl(this HttpWebRequest webReq, string method, string requestBody, string contentType,
471+
string accept, Action<HttpWebRequest> requestFilter, Action<HttpWebResponse> responseFilter)
472+
{
458473
if (method != null)
459474
webReq.Method = method;
460475
if (contentType != null)
@@ -486,13 +501,11 @@ public static string SendStringToUrl(this string url, string method = null,
486501
responseFilter?.Invoke((HttpWebResponse)webRes);
487502
return stream.ReadToEnd(UseEncoding);
488503
}
489-
490-
public static async Task<string> SendStringToUrlAsync(this string url, string method = null,
491-
string requestBody = null,
492-
string contentType = null, string accept = "*/*", Action<HttpWebRequest> requestFilter = null,
493-
Action<HttpWebResponse> responseFilter = null, CancellationToken token = default)
504+
505+
public static async Task<string> SendStringToUrlAsync(this HttpWebRequest webReq,
506+
string method, string requestBody, string contentType, string accept,
507+
Action<HttpWebRequest> requestFilter, Action<HttpWebResponse> responseFilter)
494508
{
495-
var webReq = (HttpWebRequest)WebRequest.Create(url);
496509
if (method != null)
497510
webReq.Method = method;
498511
if (contentType != null)
@@ -583,6 +596,21 @@ public static byte[] SendBytesToUrl(this string url, string method = null,
583596
Action<HttpWebRequest> requestFilter = null, Action<HttpWebResponse> responseFilter = null)
584597
{
585598
var webReq = (HttpWebRequest)WebRequest.Create(url);
599+
return SendBytesToUrl(webReq, method, requestBody, contentType, accept, requestFilter, responseFilter);
600+
}
601+
602+
public static async Task<byte[]> SendBytesToUrlAsync(this string url, string method = null,
603+
byte[] requestBody = null, string contentType = null, string accept = "*/*",
604+
Action<HttpWebRequest> requestFilter = null, Action<HttpWebResponse> responseFilter = null,
605+
CancellationToken token = default)
606+
{
607+
var webReq = (HttpWebRequest)WebRequest.Create(url);
608+
return await SendBytesToUrlAsync(webReq, method, requestBody, contentType, accept, requestFilter, responseFilter, token);
609+
}
610+
611+
public static byte[] SendBytesToUrl(this HttpWebRequest webReq, string method, byte[] requestBody, string contentType,
612+
string accept, Action<HttpWebRequest> requestFilter, Action<HttpWebResponse> responseFilter)
613+
{
586614
if (method != null)
587615
webReq.Method = method;
588616

@@ -611,13 +639,10 @@ public static byte[] SendBytesToUrl(this string url, string method = null,
611639
using var stream = webRes.GetResponseStream();
612640
return stream.ReadFully();
613641
}
614-
615-
public static async Task<byte[]> SendBytesToUrlAsync(this string url, string method = null,
616-
byte[] requestBody = null, string contentType = null, string accept = "*/*",
617-
Action<HttpWebRequest> requestFilter = null, Action<HttpWebResponse> responseFilter = null,
618-
CancellationToken token = default)
642+
643+
public static async Task<byte[]> SendBytesToUrlAsync(this HttpWebRequest webReq, string method, byte[] requestBody,
644+
string contentType, string accept, Action<HttpWebRequest> requestFilter, Action<HttpWebResponse> responseFilter, CancellationToken token)
619645
{
620-
var webReq = (HttpWebRequest)WebRequest.Create(url);
621646
if (method != null)
622647
webReq.Method = method;
623648
if (contentType != null)
@@ -646,7 +671,7 @@ public static async Task<byte[]> SendBytesToUrlAsync(this string url, string met
646671
using var stream = webRes.GetResponseStream();
647672
return await stream.ReadFullyAsync(token).ConfigAwait();
648673
}
649-
674+
650675
public static Stream GetStreamFromUrl(this string url, string accept = "*/*",
651676
Action<HttpWebRequest> requestFilter = null, Action<HttpWebResponse> responseFilter = null)
652677
{
@@ -779,11 +804,9 @@ public static async Task<Stream> SendStreamToUrlAsync(this string url, string me
779804
try
780805
{
781806
var webReq = (HttpWebRequest)WebRequest.Create(url);
782-
using (var webRes = PclExport.Instance.GetResponse(webReq))
783-
{
784-
var httpRes = webRes as HttpWebResponse;
785-
return httpRes?.StatusCode;
786-
}
807+
using var webRes = PclExport.Instance.GetResponse(webReq);
808+
var httpRes = webRes as HttpWebResponse;
809+
return httpRes?.StatusCode;
787810
}
788811
catch (Exception ex)
789812
{

0 commit comments

Comments
 (0)