/// <summary> /// Download data from a URL using GET method /// </summary> /// <param name="url">Url</param> /// <returns>Raw bytes</returns> public Task <byte[]> DownloadDataAsync(string url) { Interlocked.Increment(ref requestCount); using (WebClient client = new WebClient()) { Assembly a = (Assembly.GetEntryAssembly() ?? IPBanService.GetIPBanAssembly()); client.UseDefaultCredentials = true; client.Headers["User-Agent"] = a.GetName().Name; return(client.DownloadDataTaskAsync(url)); } }
public Task <byte[]> MakeRequestAsync(string url, string postJson = null, params KeyValuePair <string, string>[] headers) { Interlocked.Increment(ref requestCount); using (WebClient client = new WebClient()) { Assembly a = (Assembly.GetEntryAssembly() ?? IPBanService.GetIPBanAssembly()); client.UseDefaultCredentials = true; client.Headers["User-Agent"] = a.GetName().Name; foreach (KeyValuePair <string, string> header in headers) { client.Headers[header.Key] = header.Value; } if (string.IsNullOrWhiteSpace(postJson)) { return(client.DownloadDataTaskAsync(url)); } client.Headers["Content-Type"] = "application/json"; return(client.UploadDataTaskAsync(url, "POST", Encoding.UTF8.GetBytes(postJson))); } }