| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="HttpWebRequest"/> |
| | 5 | | /// </summary> |
| | 6 | | public static class HttpWebRequestExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Downloads to file. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="request">The request.</param> |
| | 12 | | /// <param name="path">The path.</param> |
| | 13 | | public static void DownloadToFile(this HttpWebRequest? request, string? path) => |
| 0 | 14 | | request.DownloadToFile(path, null, bypassProxy: true); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Downloads to file. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="request">The request.</param> |
| | 20 | | /// <param name="path">The path.</param> |
| | 21 | | /// <param name="proxyLocation">The proxy location.</param> |
| | 22 | | /// <param name="bypassProxy">if set to <c>true</c> [bypass proxy].</param> |
| | 23 | | public static void DownloadToFile(this HttpWebRequest? request, string? path, Uri? proxyLocation, bool bypassProxy) |
| 0 | 24 | | { |
| 0 | 25 | | if (request == null) return; |
| 0 | 26 | | path.ThrowWhenNullOrWhiteSpace(); |
| | 27 | |
|
| 0 | 28 | | var buffer = new byte[32768]; |
| 0 | 29 | | var fileName = Path.GetFileName(path); |
| | 30 | |
|
| 0 | 31 | | request.Headers.Add("Content-Disposition", $"attachment; filename={fileName}"); |
| | 32 | |
|
| 0 | 33 | | var response = request |
| 0 | 34 | | .WithProxy(proxyLocation, bypassProxy) |
| 0 | 35 | | .ToHttpWebResponse() |
| 0 | 36 | | .ToReferenceTypeValueOrThrow(); |
| | 37 | |
|
| 0 | 38 | | var stream = response.GetResponseStream(); |
| | 39 | | try |
| 0 | 40 | | { |
| 0 | 41 | | using var fs = File.Create(path); |
| | 42 | |
|
| | 43 | | int bytesRead; |
| 0 | 44 | | while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) |
| 0 | 45 | | { |
| 0 | 46 | | fs.Write(buffer, 0, bytesRead); |
| 0 | 47 | | } |
| 0 | 48 | | } |
| | 49 | | finally |
| 0 | 50 | | { |
| 0 | 51 | | stream.Close(); |
| 0 | 52 | | response.Close(); |
| 0 | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Downloads to string. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="request">The request.</param> |
| | 60 | | public static string? DownloadToString(this HttpWebRequest? request) => |
| 0 | 61 | | request.DownloadToString(null, bypassProxy: true); |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Downloads to string. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="request">The request.</param> |
| | 67 | | /// <param name="proxyLocation">The proxy location</param> |
| | 68 | | /// <param name="bypassProxy">when <c>true</c>, bypass proxy</param> |
| | 69 | | public static string? DownloadToString(this HttpWebRequest? request, Uri? proxyLocation, bool bypassProxy) |
| 0 | 70 | | { |
| 0 | 71 | | if (request == null) return null; |
| | 72 | |
|
| | 73 | | string? content; |
| 0 | 74 | | var response = request |
| 0 | 75 | | .WithProxy(proxyLocation, bypassProxy) |
| 0 | 76 | | .ToHttpWebResponse() |
| 0 | 77 | | .ToReferenceTypeValueOrThrow(); |
| | 78 | | try |
| 0 | 79 | | { |
| 0 | 80 | | using var sr = new StreamReader(response.GetResponseStream()); |
| | 81 | |
|
| 0 | 82 | | content = sr.ReadToEnd(); |
| 0 | 83 | | } |
| | 84 | | finally |
| 0 | 85 | | { |
| 0 | 86 | | response.Close(); |
| 0 | 87 | | } |
| | 88 | |
|
| 0 | 89 | | return content; |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Posts the form. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="request">The request.</param> |
| | 96 | | /// <param name="postData">The post data.</param> |
| | 97 | | public static string? PostForm(this HttpWebRequest? request, Hashtable? postData) => |
| 0 | 98 | | request.PostForm(postData, proxyLocation: null, bypassProxy: true); |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Posts the form. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="request">The request.</param> |
| | 104 | | /// <param name="postData">The post data.</param> |
| | 105 | | /// <param name="proxyLocation">The proxy location.</param> |
| | 106 | | /// <param name="bypassProxy">if set to <c>true</c> [bypass proxy].</param> |
| | 107 | | public static string? PostForm(this HttpWebRequest? request, Hashtable? postData, |
| | 108 | | Uri? proxyLocation, bool bypassProxy) |
| 0 | 109 | | { |
| 0 | 110 | | if (request == null) return null; |
| 0 | 111 | | ArgumentNullException.ThrowIfNull(postData); |
| | 112 | |
|
| 0 | 113 | | var postParams = GetPostData(postData); |
| | 114 | |
|
| 0 | 115 | | request |
| 0 | 116 | | .WithProxy(proxyLocation, bypassProxy) |
| 0 | 117 | | .WithRequestBody(postParams, "POST", MimeTypes.ApplicationFormUrlEncoded); |
| | 118 | |
|
| 0 | 119 | | var response = request.DownloadToString(); |
| | 120 | |
|
| 0 | 121 | | return response; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Posts the XML. |
| | 126 | | /// </summary> |
| | 127 | | /// <param name="request">The request.</param> |
| | 128 | | /// <param name="xmlFragment">The XML fragment.</param> |
| | 129 | | public static string? PostXml(this HttpWebRequest? request, string? xmlFragment) => |
| 0 | 130 | | request.PostXml(xmlFragment, null, bypassProxy: true); |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Posts the XML. |
| | 134 | | /// </summary> |
| | 135 | | /// <param name="request">The request.</param> |
| | 136 | | /// <param name="xmlFragment">The XML fragment.</param> |
| | 137 | | /// <param name="proxyLocation">The proxy location.</param> |
| | 138 | | /// <param name="bypassProxy">if set to <c>true</c> [bypass proxy].</param> |
| | 139 | | public static string? PostXml(this HttpWebRequest? request, string? xmlFragment, Uri? proxyLocation, |
| | 140 | | bool bypassProxy) |
| 0 | 141 | | { |
| 0 | 142 | | if (request == null) return null; |
| | 143 | |
|
| 0 | 144 | | request |
| 0 | 145 | | .WithProxy(proxyLocation, bypassProxy) |
| 0 | 146 | | .WithRequestBody(xmlFragment, "POST", MimeTypes.TextXml); |
| | 147 | |
|
| 0 | 148 | | var response = request.DownloadToString(); |
| | 149 | |
|
| 0 | 150 | | return response; |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Converts the <see cref="HttpWebRequest"/> into a HTTP status code. |
| | 155 | | /// </summary> |
| | 156 | | /// <param name="request">The request.</param> |
| | 157 | | public static HttpStatusCode ToHttpStatusCode(this HttpWebRequest? request) |
| 0 | 158 | | { |
| 0 | 159 | | if (request == null) return HttpStatusCode.Unused; |
| | 160 | |
|
| | 161 | | HttpStatusCode code; |
| | 162 | | try |
| 0 | 163 | | { |
| 0 | 164 | | using var response = request.ToHttpWebResponse(); |
| | 165 | |
|
| 0 | 166 | | code = response?.StatusCode ?? HttpStatusCode.Unused; |
| 0 | 167 | | } |
| 0 | 168 | | catch (WebException ex) |
| 0 | 169 | | { |
| 0 | 170 | | using HttpWebResponse? response = ex.Response as HttpWebResponse; |
| | 171 | |
|
| 0 | 172 | | code = response?.StatusCode ?? HttpStatusCode.Unused; |
| 0 | 173 | | } |
| | 174 | |
|
| 0 | 175 | | return code; |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Converts the <see cref="HttpWebRequest"/> into a HTTP web response. |
| | 180 | | /// </summary> |
| | 181 | | /// <param name="request">The request.</param> |
| | 182 | | public static HttpWebResponse? ToHttpWebResponse(this HttpWebRequest? request) => |
| 0 | 183 | | request?.GetResponse() as HttpWebResponse; |
| | 184 | |
|
| | 185 | | /// <summary> |
| | 186 | | /// Returns the <see cref="HttpWebRequest" /> |
| | 187 | | /// with a <see cref="WebProxy" />. |
| | 188 | | /// </summary> |
| | 189 | | /// <param name="request">The request.</param> |
| | 190 | | /// <param name="proxyLocation">The proxy location.</param> |
| | 191 | | /// <param name="bypassProxy">if set to <c>true</c> [bypass proxy].</param> |
| | 192 | | public static HttpWebRequest? WithProxy(this HttpWebRequest? request, Uri? proxyLocation, bool bypassProxy) |
| 0 | 193 | | { |
| 0 | 194 | | if (request == null) return null; |
| | 195 | |
|
| 0 | 196 | | request.Credentials = CredentialCache.DefaultNetworkCredentials; |
| | 197 | |
|
| 0 | 198 | | if (proxyLocation != null && (!string.IsNullOrWhiteSpace(proxyLocation.AbsoluteUri))) |
| 0 | 199 | | request.Proxy = new WebProxy(proxyLocation, bypassProxy); |
| | 200 | |
|
| 0 | 201 | | return request; |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | /// <summary> |
| | 205 | | /// Returns <see cref="HttpWebRequest" /> with the request body. |
| | 206 | | /// </summary> |
| | 207 | | /// <param name="request">The request.</param> |
| | 208 | | /// <param name="requestBody">The request body.</param> |
| | 209 | | /// <param name="requestMethod">The request method.</param> |
| | 210 | | public static HttpWebRequest? WithRequestBody(this HttpWebRequest? request, string? requestBody, |
| 0 | 211 | | string? requestMethod) => request.WithRequestBody(requestBody, requestMethod, contentType: null); |
| | 212 | |
|
| | 213 | | /// <summary> |
| | 214 | | /// Returns <see cref="HttpWebRequest" /> with the request body. |
| | 215 | | /// </summary> |
| | 216 | | /// <param name="request">The request.</param> |
| | 217 | | /// <param name="requestBody">The request body.</param> |
| | 218 | | /// <param name="requestMethod">The request method.</param> |
| | 219 | | /// <param name="contentType">The request content type.</param> |
| | 220 | | public static HttpWebRequest? WithRequestBody(this HttpWebRequest? request, string? requestBody, |
| | 221 | | string? requestMethod, string? contentType) |
| 0 | 222 | | { |
| 0 | 223 | | if (request == null) return null; |
| | 224 | |
|
| 0 | 225 | | requestBody.ThrowWhenNullOrWhiteSpace(); |
| 0 | 226 | | requestMethod.ThrowWhenNullOrWhiteSpace(); |
| | 227 | |
|
| 0 | 228 | | if (string.IsNullOrWhiteSpace(contentType)) contentType = MimeTypes.TextPlain; |
| | 229 | |
|
| 0 | 230 | | request.Method = requestMethod; |
| 0 | 231 | | request.ContentType = contentType; |
| | 232 | |
|
| 0 | 233 | | byte[] body = Encoding.UTF8.GetBytes(requestBody); |
| | 234 | |
|
| 0 | 235 | | using Stream dataStream = request.GetRequestStream(); |
| | 236 | |
|
| 0 | 237 | | dataStream.Write(body, 0, requestBody.Length); |
| | 238 | |
|
| 0 | 239 | | return request; |
| 0 | 240 | | } |
| | 241 | |
|
| | 242 | | static string GetPostData(Hashtable postData) |
| 0 | 243 | | { |
| 0 | 244 | | var sb = new StringBuilder(); |
| 0 | 245 | | string s = sb.ToString(); |
| | 246 | |
|
| 0 | 247 | | foreach (DictionaryEntry entry in postData) |
| 0 | 248 | | { |
| 0 | 249 | | s = (string.IsNullOrWhiteSpace(s)) |
| 0 | 250 | | ? string.Format(CultureInfo.InvariantCulture, "{0}={1}", entry.Key, entry.Value) |
| 0 | 251 | | : string.Format(CultureInfo.InvariantCulture, "&{0}={1}", entry.Key, entry.Value); |
| 0 | 252 | | sb.Append(s); |
| 0 | 253 | | } |
| | 254 | |
|
| 0 | 255 | | return sb.ToString(); |
| 0 | 256 | | } |
| | 257 | | } |