/// <summary> /// read from a website; /// used to check for available patches /// </summary> /// <param name="url"></param> /// <returns></returns> public static string ReadWebsite(string url) { string ReturnValue = null; byte[] buf; WebClientWithSession WClient = GetNewWebClient(url); if (TLogging.DebugLevel > 0) { string urlToLog = url; if (url.Contains("password")) { urlToLog = url.Substring(0, url.IndexOf("?")) + "?..."; } TLogging.Log(urlToLog); } try { buf = WClient.Get(url); if ((buf != null) && (buf.Length > 0)) { ReturnValue = Encoding.ASCII.GetString(buf, 0, buf.Length); } else { TLogging.Log("server did not return anything? timeout?"); } StoreSessionCookie(WClient.CookieContainer, url); } catch (System.Net.WebException e) { if (url.Contains("?")) { // do not show passwords in the log file which could be encoded in the parameters TLogging.Log("Trying to download: " + url.Substring(0, url.IndexOf("?")) + "?..." + Environment.NewLine + e.ToString(), TLoggingType.ToLogfile); } else { TLogging.Log("Trying to download: " + url + Environment.NewLine + e.ToString(), TLoggingType.ToLogfile); } } return(ReturnValue); }
private static string WebClientUploadValues(string url, NameValueCollection parameters, int ANumberOfAttempts = 0) { byte[] buf; WebClientWithSession WClient = GetNewWebClient(url); try { buf = WClient.Get(url, parameters); } catch (System.Net.WebException ex) { TLogging.LogAtLevel(0, "WebClientUploadValues WebException: " + ex.ToString()); HttpWebResponse httpWebResponse = (HttpWebResponse)ex.Response; if (httpWebResponse != null) { if (httpWebResponse.StatusCode == HttpStatusCode.NotFound) { throw new Exception(SESSION_ALREADY_CLOSED); } if (httpWebResponse.StatusCode == HttpStatusCode.InternalServerError) { // do not retry if code 500 returns throw; } } if (ANumberOfAttempts > 0) { // sleep for half a second System.Threading.Thread.Sleep(500); return(WebClientUploadValues(url, parameters, ANumberOfAttempts - 1)); } throw; } StoreSessionCookie(WClient.CookieContainer, url); if ((buf != null) && (buf.Length > 0)) { return(Encoding.ASCII.GetString(buf, 0, buf.Length)); } return(String.Empty); }
/// <summary> /// download a patch or other file from a website; /// used for patching the program /// </summary> /// <param name="url"></param> /// <param name="filename"></param> /// <returns></returns> public static Boolean DownloadFile(string url, string filename) { WebClientWithSession WClient = GetNewWebClient(url); try { WClient.DownloadFile(url, filename); StoreSessionCookie(WClient.CookieContainer, url); return(true); } catch (Exception e) { TLogging.Log(e.Message + " url: " + url + " filename: " + filename); } return(false); }