static public void LogTextColor(string text, Color color)
        {
            try
            {
                static_richTextBox.SelectionStart  = static_richTextBox.TextLength;
                static_richTextBox.SelectionLength = 0;

                static_richTextBox.SelectionColor = color;
                static_richTextBox.AppendText(text);
                static_richTextBox.SelectionColor = static_richTextBox.ForeColor;

                static_richTextBox.SelectionStart  = static_richTextBox.Text.Length;
                static_richTextBox.SelectionLength = 0;
                static_richTextBox.ScrollToCaret();
                static_richTextBox.Refresh();


                HttpCom.WriteLog(text, "");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception SystemClass: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);
            }
        }
Beispiel #2
0
        public static List <JournalScan> GetBodiesList(long edsmid)
        {
            try
            {
                if (DictEDSMBodies != null && DictEDSMBodies.ContainsKey(edsmid))  // Cache EDSM bidies during run of EDD.
                {
                    return(DictEDSMBodies[edsmid]);
                }

                List <JournalScan> bodies = new List <JournalScan>();

                EDSMClass edsm = new EDSMClass();

                JObject jo = edsm.GetBodies(edsmid);  // Colonia

                if (jo != null)
                {
                    foreach (JObject bodie in jo["bodies"])
                    {
                        try
                        {
                            EDSMClass.ConvertFromEDSMBodies(bodie);
                            JournalScan js = new JournalScan(bodie);
                            js.EdsmID = edsmid;

                            bodies.Add(js);
                        }
                        catch (Exception ex)
                        {
                            HttpCom.WriteLog($"Exception Loop: {ex.Message}", "");
                            HttpCom.WriteLog($"ETrace: {ex.StackTrace}", "");
                            Trace.WriteLine($"Exception Loop: {ex.Message}");
                            Trace.WriteLine($"ETrace: {ex.StackTrace}");
                        }
                    }
                    DictEDSMBodies[edsmid] = bodies;
                    return(bodies);
                }

                DictEDSMBodies[edsmid] = null;
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Exception: {ex.Message}");
                Trace.WriteLine($"ETrace: {ex.StackTrace}");
                return(null);
            }
            return(null);
        }
        /**
         * Obtain the response data from an HTTP web response
         */
        private string getResponseData(HttpWebResponse response)
        {
            // Obtain and parse our response
            var encoding = response.CharacterSet == ""
                    ? Encoding.UTF8
                    : Encoding.GetEncoding(response.CharacterSet);

            Trace.WriteLine("Reading response");
            using (var stream = response.GetResponseStream())
            {
                var    reader = new StreamReader(stream, encoding);
                string data   = reader.ReadToEnd();
                if (data == null || data.Trim() == "")
                {
                    HttpCom.WriteLog("Companion No data returned", "");
                    return(null);
                }
                HttpCom.WriteLog("Companion Data is ", data);
                return(data);
            }
        }
        // Obtain a response, ensuring that we obtain the response's cookies
        private HttpWebResponse GetResponse(HttpWebRequest request)
        {
            HttpCom.WriteLog("Companion Requesting ", request.RequestUri.ToNullSafeString());
            //Trace.WriteLine("Requesting " + request.RequestUri);
            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException wex)
            {
                Trace.WriteLine("Failed to obtain response, error code " + wex.Status);
                return(null);
            }
            HttpCom.WriteLog("Companion Response ", JsonConvert.SerializeObject(response));
            //Trace.WriteLine("Response is " + JsonConvert.SerializeObject(response));

            UpdateCredentials(response);
            Credentials.ToFile();

            return(response);
        }
Beispiel #5
0
        public bool DownloadFile(string url, string filename, out bool newfile)
        {
            var etagFilename    = filename + ".etag";
            var tmpFilename     = filename + ".tmp";
            var tmpEtagFilename = etagFilename + ".tmp";

            newfile = false;

            HttpCom.WriteLog("DownloadFile", url);
            var request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.UserAgent = "EDDiscovery v" + Assembly.GetExecutingAssembly().FullName.Split(',')[1].Split('=')[1];
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            if (File.Exists(etagFilename))
            {
                var etag = File.ReadAllText(etagFilename);
                if (etag != "")
                {
                    request.Headers[HttpRequestHeader.IfNoneMatch] = etag;
                }
            }

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                HttpCom.WriteLog("Response", response.StatusCode.ToString());

                File.WriteAllText(tmpEtagFilename, response.Headers[HttpResponseHeader.ETag]);
                var destFileStream = File.Open(tmpFilename, FileMode.Create, FileAccess.Write);
                response.GetResponseStream().CopyTo(destFileStream);

                destFileStream.Close();
                response.Close();

                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                if (File.Exists(etagFilename))
                {
                    File.Delete(etagFilename);
                }

                File.Move(tmpFilename, filename);
                File.Move(tmpEtagFilename, etagFilename);

                newfile = true;
                return(true);
            }
            catch (WebException ex)
            {
                var code = ((HttpWebResponse)ex.Response).StatusCode;
                if (code == HttpStatusCode.NotModified)
                {
                    System.Diagnostics.Trace.WriteLine("EDDB: " + filename + " up to date (etag).");
                    HttpCom.WriteLog(filename, "up to date (etag).");
                    return(true);
                }
                System.Diagnostics.Trace.WriteLine("DownloadFile Exception:" + ex.Message);
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                HttpCom.WriteLog("Exception", ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("DownloadFile Exception:" + ex.Message);
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                HttpCom.WriteLog("DownloadFile Exception", ex.Message);
                return(false);
            }
        }
Beispiel #6
0
        private static T ProcessDownload <T>(string url, string filename, Action <bool, Stream> processor, Func <HttpWebRequest, Func <Func <HttpWebResponse>, bool>, T> doRequest)
        {
            var etagFilename    = filename == null ? null : filename + ".etag";
            var tmpEtagFilename = filename == null ? null : etagFilename + ".tmp";

            HttpCom.WriteLog("DownloadFile", url);
            var request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.UserAgent = "EDDiscovery v" + Assembly.GetExecutingAssembly().FullName.Split(',')[1].Split('=')[1];
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            if (filename != null && File.Exists(etagFilename))
            {
                var etag = File.ReadAllText(etagFilename);
                if (etag != "")
                {
                    request.Headers[HttpRequestHeader.IfNoneMatch] = etag;
                }
                else
                {
                    request.IfModifiedSince = File.GetLastWriteTimeUtc(etagFilename);
                }
            }

            return(doRequest(request, (getResponse) =>
            {
                try
                {
                    using (var response = getResponse())
                    {
                        HttpCom.WriteLog("Response", response.StatusCode.ToString());

                        File.WriteAllText(tmpEtagFilename, response.Headers[HttpResponseHeader.ETag]);
                        File.SetLastWriteTimeUtc(tmpEtagFilename, response.LastModified.ToUniversalTime());
                        using (var httpStream = response.GetResponseStream())
                        {
                            processor(true, httpStream);
                            File.Delete(etagFilename);
                            File.Move(tmpEtagFilename, etagFilename);
                            return true;
                        }
                    }
                }
                catch (WebException ex)
                {
                    if ((HttpWebResponse)ex.Response == null)
                    {
                        return false;
                    }

                    var code = ((HttpWebResponse)ex.Response).StatusCode;
                    if (code == HttpStatusCode.NotModified)
                    {
                        System.Diagnostics.Trace.WriteLine("EDDB: " + filename + " up to date (etag).");
                        HttpCom.WriteLog(filename, "up to date (etag).");
                        using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            processor(false, stream);
                        }
                        return true;
                    }
                    System.Diagnostics.Trace.WriteLine("DownloadFile Exception:" + ex.Message);
                    System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                    HttpCom.WriteLog("Exception", ex.Message);
                    return false;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine("DownloadFile Exception:" + ex.Message);
                    System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                    HttpCom.WriteLog("DownloadFile Exception", ex.Message);
                    return false;
                }
            }));
        }