public static void createGetRequest(string requestUrl, downloadFile callback, bool setCookie, object metadata) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl); if (setCookie) { addToken(request); } request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString(); request.BeginGetResponse(GetRequestCallback, new object[] { request, callback, metadata }); }
static void GetRequestCallback(IAsyncResult result) { object[] vars = (object[])result.AsyncState; HttpWebRequest request = vars[0] as HttpWebRequest; JObject jObject = null; string data = ""; byte[] fileBytes = null; if (request != null) { try { WebResponse response = request.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); if (string.Equals(response.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { data = decompressResponse(responseStream); } else { if (vars[1] is postResponseFunction) { using (var reader = new StreamReader(responseStream)) { data = reader.ReadToEnd(); } jObject = JObject.Parse(data); } else if (vars[1] is downloadFile) { using (BinaryReader br = new BinaryReader(responseStream)) { fileBytes = br.ReadBytes((int)responseStream.Length); } } } } catch (IOException ioe) { } catch (WebException we) { } catch (JsonException je) { } catch (Exception e) { } finally { if (vars[1] is postResponseFunction) { postResponseFunction finalCallbackFunction = vars[1] as postResponseFunction; finalCallbackFunction(jObject); } else if (vars[1] is downloadFile) { downloadFile finalCallbackFunction = vars[1] as downloadFile; finalCallbackFunction(fileBytes, vars[2] as object); } } } }
protected void rptRecomEnt_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "upload") { downloadFile df = new downloadFile(); string filename = "/PolicyFile/" + e.CommandArgument; df.download(e.CommandArgument.ToString(),filename, Page); } }