public async Task <ArchiveResponse> DownloadArchive(string docid, DocumentType type, int retry) { ArchiveResponse response = new ArchiveResponse(); string url = string.Format("/api/{0}/documents/{1}?type={2}", Version, docid, (int)type); int i = 0; do { if (i > 0) { SaveLog($" retry[{i}] {docid} {type}"); Debug.Write($"retry Download[{i}] "); await Task.Delay(2000); } try { debug.ProgramCodeInfo.SetDebugQueue(); using (HttpResponseMessage res = await client.GetAsync(url)) { debug.ProgramCodeInfo.SetDebugQueue(); string filename = res.Content.Headers.ContentDisposition.FileName.Replace("\"", ""); //string filename = $"{docid}_{(int)type}"; System.Net.Http.Headers.MediaTypeHeaderValue contenttype = res.Content.Headers.ContentType; if (filename == "404.json") { filename = url; } #pragma warning disable CS4014 SaveLog(GetLog(res.StatusCode, RequestType.Archive, contenttype, filename)); #pragma warning restore CS4014 using (Stream stream = await res.Content.ReadAsStreamAsync()) { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); byte[] buffer = ms.ToArray(); stream.Flush(); response.Update(buffer, res.StatusCode, filename, contenttype); debug.ProgramCodeInfo.SetDebugQueue(); return(response); } } } } catch (TaskCanceledException ex) { response.Update(ex); #pragma warning disable CS4014 SaveLog(GetLog(response)); #pragma warning restore CS4014 debug.ProgramCodeInfo.SetDebugQueue(); } catch (Exception ex) { response.Update(ex); #pragma warning disable CS4014 SaveLog(GetLog(response)); #pragma warning restore CS4014 debug.ProgramCodeInfo.SetDebugQueue(); return(response); } i++; } while (i <= retry); return(response); }
public async Task <HttpResponseMessage> RequestDownload(string docid, DocumentType type) { string url = string.Format("/api/{0}/documents/{1}?type={2}", Version, docid, (int)type); try { return(await client.GetAsync(url)); } catch (Exception ex) { ArchiveResponse response = new ArchiveResponse(); response.Update(ex); #pragma warning disable CS4014 SaveLog(GetLog(response)); #pragma warning restore CS4014 return(null); } }