private APIResponse LoadAPI(string url) { DateTime lastModified = old_date; APIResponse result = null; StorageEntry se = cache.GetText(url); if (se != null) { lastModified = se.LastModified; } HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url); wr.IfModifiedSince = lastModified; wr.UserAgent = ChanArchiver.Program.get_random_user_agent(); WebResponse wbr = null; try { byte[] data; wbr = wr.GetResponse(); using (Stream s = wbr.GetResponseStream()) { int iByteSize = 0; byte[] byteBuffer = new byte[2048]; using (MemoryStream MemIo = new MemoryStream()) { while ((iByteSize = s.Read(byteBuffer, 0, 2048)) > 0) { MemIo.Write(byteBuffer, 0, iByteSize); ChanArchiver.NetworkUsageCounter.Add_ApiConsumed(iByteSize); } data = MemIo.ToArray(); } } string text = System.Text.Encoding.UTF8.GetString(data.ToArray()); string lm = wbr.Headers["Last-Modified"]; DateTime lmm = DateTime.Parse(lm); cache.StoreText(url, text, lmm); result = new APIResponse(text, APIResponse.ErrorType.NoError); } catch (WebException wex) { HttpWebResponse httpResponse = wex.Response as HttpWebResponse; if (httpResponse != null) { if (httpResponse.StatusCode == HttpStatusCode.NotModified) { if (se != null) { result = new APIResponse(se.Text, APIResponse.ErrorType.NoError); } } else if (httpResponse.StatusCode == HttpStatusCode.NotFound) { result = new APIResponse(null, APIResponse.ErrorType.NotFound); //delete cache entry since resource has 404'ed cache.ClearText(url); } else { result = new APIResponse(wex.Message, APIResponse.ErrorType.Other); //throw wex; } } else { result = new APIResponse(wex.Message, APIResponse.ErrorType.Other); //throw wex; } } if (wbr != null) { wbr.Close(); } return(result); }