public static string GetJSON(string InputURL, DateTime ModifiedSince = default(DateTime)) { try { string JSONOutput = null; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(InputURL); Request.UserAgent = YChanEx.Advanced.Default.UserAgent; Request.Method = "GET"; if (ModifiedSince != default(DateTime)) { Request.IfModifiedSince = ModifiedSince; } var Response = (HttpWebResponse)Request.GetResponse(); var ResponseStream = Response.GetResponseStream(); using (StreamReader Reader = new StreamReader(ResponseStream)) { string JSONString = Reader.ReadToEnd(); byte[] JSONBytes = Encoding.ASCII.GetBytes(JSONString); using (var MemoryStream = new MemoryStream(JSONBytes)) { var Quotas = new XmlDictionaryReaderQuotas(); var JSONReader = JsonReaderWriterFactory.CreateJsonReader(MemoryStream, Quotas); var XMLJSON = XDocument.Load(JSONReader); JSONOutput = XMLJSON.ToString(); MemoryStream.Flush(); MemoryStream.Close(); } } ResponseStream.Dispose(); Response.Dispose(); GC.Collect(); if (JSONOutput != null) { if (JSONOutput != EmptyXML) { return(JSONOutput); } } return(null); } catch (WebException WebEx) { ErrorLog.ReportWebException(WebEx, InputURL); return(null); } catch (Exception ex) { ErrorLog.ReportException(ex); return(null); } }
public static bool DownloadFile(string FileURL, string Destination, string FileName, bool RequireCookie = false, string RequiredCookie = null) { try { if (!Directory.Exists(Destination)) { Directory.CreateDirectory(Destination); } using (WebClientMethod wc = new WebClientMethod()) { wc.Method = "GET"; wc.Headers.Add(HttpRequestHeader.UserAgent, YChanEx.Advanced.Default.UserAgent); if (RequireCookie && !string.IsNullOrEmpty(RequiredCookie)) { wc.Headers.Add(HttpRequestHeader.Cookie, RequiredCookie); } string FullFileName = Destination + "\\" + FileName; if (FullFileName.Length > 255 && !Downloads.Default.AllowFileNamesGreaterThan255) { string FileExtension = FileName.Split('.')[FileName.Split('.').Length - 1]; string OldFileName = FileName; FileName = FullFileName.Substring(0, (255 - FileExtension.Length - 1)); File.WriteAllText(Destination + "\\" + FileName + ".txt", OldFileName); FullFileName = Destination + "\\" + FileName; } if (!File.Exists(FullFileName)) { wc.DownloadFile(FileURL, FullFileName); } } return(true); } catch (WebException WebEx) { ErrorLog.ReportWebException(WebEx, FileURL); return(false); } catch (Exception Ex) { ErrorLog.ReportException(Ex); return(false); } }
public static string GetHTML(string InputURL, bool RequireCookie = false, string RequiredCookie = null) { try { using (WebClient wc = new WebClient()) { wc.Headers.Add("User-Agent: " + YChanEx.Advanced.Default.UserAgent); if (RequireCookie && !string.IsNullOrEmpty(RequiredCookie)) { wc.Headers.Add(HttpRequestHeader.Cookie, RequiredCookie); } return(wc.DownloadString(InputURL)); } } catch (WebException WebEx) { ErrorLog.ReportWebException(WebEx, InputURL); return(null); } catch (Exception Ex) { ErrorLog.ReportException(Ex); return(null); } }