public void Initialize(UploadServer server) { Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Initialize Enter"); errorMsg = ""; UpdateCommodNameToIndexMap(server); UpdateStallTypeToAbbrevMap(); isInitialized = true; Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Initialize Return"); }
private void UploadFile(UploadServer server) { using (WebClient client = new WebClient()) { client.Headers.Add("Content-Type", "application/json"); using (Stream fileStream = File.OpenRead(appDataDir + "\\Commodities.json")) using (Stream requestStream = client.OpenWrite(server.UploadUrl, "POST")) { fileStream.CopyTo(requestStream); } } }
public void Upload(List <BidData> bidData, UploadServer server, System.Windows.Forms.WebBrowser webBrowser) { errorMsg = ""; if (bidData == null) { errorMsg = "No bid data to upload"; return; } if (bidData.Count <= 0) { errorMsg = "No bid data to upload"; return; } if (!IsInitialized) { Initialize(server); } if (errorMsg.Length > 0) { return; } CommodMap commodMap = null; string url = server.UploadUrl.ToString() + "?action=upload&biddata="; //string url = ConfigurationSettings.AppSettings["HostUrl"].ToString() + "/upload.php?action=upload&biddata="; foreach (BidData b in bidData) { if (commodInfo.CommodMapsDict.TryGetValue(b.CommodityName, out commodMap)) { url += commodMap.Index.ToString(); url += "," + b.HighBid.ToString(); url += "," + b.Qty.ToString() + ","; } else { errorMsg = "Could not find commodity in commodity map: " + b.CommodityName; return; } } webBrowser.Url = new Uri(url); // Upload data }
private void LoadServerInfo() { comboBoxUploadServer.Items.Clear(); int i = 1; while (ConfigurationSettings.AppSettings["ServerHomeUrl" + i.ToString()] != null) { UploadServer temp = new UploadServer(); temp.HomeUrl = new Uri(ConfigurationSettings.AppSettings["ServerHomeUrl" + i.ToString()]); temp.UploadUrl = new Uri(ConfigurationSettings.AppSettings["ServerUploadUrl" + i.ToString()]); temp.ServerType = (UploadServerType)Convert.ToInt32(ConfigurationSettings.AppSettings["ServerType" + i.ToString()]); comboBoxUploadServer.Items.Add(temp); i++; } if (options.SelectedUploadServer.Length <= 0) // Not initialized yet { UploadServer server = (UploadServer)comboBoxUploadServer.Items[0]; options.SelectedUploadServer = server.HomeUrl.ToString(); comboBoxUploadServer.SelectedIndex = 0; } else { // Find the selected upload server bool found = false; for (int j = 0; j < comboBoxUploadServer.Items.Count; j++) { if (((UploadServer)comboBoxUploadServer.Items[j]).HomeUrl.ToString().Equals(options.SelectedUploadServer)) { comboBoxUploadServer.SelectedIndex = j; found = true; break; } } if (!found) { comboBoxUploadServer.SelectedIndex = 0; } } }
private void UpdateUploadServer() { // Turn everything off, then selectively turn on applicable items on upload tab toolStripButtonWebHome.Visible = false; webBrowser.Url = null; webBrowser.Visible = false; buttonUpload.Visible = false; UploadServer server = (UploadServer)comboBoxUploadServer.SelectedItem; if (server.ServerType == UploadServerType.PCTB) { toolStripButtonWebHome.Visible = true; webBrowser.Visible = true; webBrowser.Url = server.UploadUrl; } else { buttonUpload.Visible = true; } }
private void UpdateCommodNameToIndexMap(UploadServer server) { bool useLocalCommodMapFile = Convert.ToBoolean(ConfigurationSettings.AppSettings["UseLocalCommodMapFile"]); if (useLocalCommodMapFile) { commodInfo = CommodInfo.Deserialize("CommodInfo.xml"); return; } string url = server.HomeUrl + "/commodmap.php"; HttpWebRequest web = (HttpWebRequest)WebRequest.Create(url); web.KeepAlive = true; web.Credentials = System.Net.CredentialCache.DefaultCredentials; WebResponse webResponse = web.GetResponse(); Stream stream2 = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(stream2); string findStart; while (!reader.EndOfStream) { findStart = reader.ReadLine(); if (findStart.Contains("<pre>")) { break; } } //string text = HttpUtility.HtmlDecode(reader.ReadToEnd()); string text = reader.ReadToEnd(); // text = text.Replace("<", "<"); // text = text.Replace(">", ">"); // text = text.Replace("< ", "<"); // text = text.Replace("> ", ">"); text = HttpUtility.HtmlDecode(text).Trim(); int index = text.IndexOf("</body>"); if (index >= 0) { text = text.Remove(index); } index = text.IndexOf("</pre>"); if (index >= 0) { text = text.Remove(index); } commodInfo = CommodInfo.Deserialize(new StringReader(text)); reader.Close(); }
public void Upload(Dictionary <string, Commodity> commods, UploadServer server, System.Windows.Forms.WebBrowser webBrowser) { Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload Enter"); if (commods == null) { errorMsg = "No commodities to upload"; return; } if (commods.Count <= 0) { errorMsg = "No commodities to upload"; return; } errorMsg = ""; if (!IsInitialized) { Initialize(server); } if (errorMsg.Length > 0) { return; } Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload UpdateLists"); this.commods = commods; List <string> commoditiesWithoutMap; UpdateLists(out commoditiesWithoutMap); if (errorMsg.Length > 0) { return; } // Create file equivalent of upload data (useful for debugging) Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload Create Capture.dat"); FileStream fs; if (Utils.traceSwitch.Level == TraceLevel.Verbose) { if (File.Exists(dataFileName)) { File.Delete(dataFileName); } fs = new FileStream(dataFileName, FileMode.CreateNew); CreateUploadStream(fs); // For debugging fs.Close(); } // Create GZip stream for upload data // MemoryStream ms = new MemoryStream(); Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload Create marketdata.gz"); string uploadFile = appDataDir + "\\marketdata.gz"; if (File.Exists(uploadFile)) { File.Delete(uploadFile); } fs = new FileStream(uploadFile, FileMode.CreateNew); GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress); CreateUploadStream(zipStream); zipStream.Close(); fs.Close(); Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload Create Upload Stream"); MemoryStream ms = new MemoryStream(); GZipStream zipMemoryStreem = new GZipStream(ms, CompressionMode.Compress); CreateUploadStream(zipMemoryStreem); zipMemoryStreem.Close(); ms.Close(); // UploadFile(); // Works Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload Upload To Server"); UploadFile(server); // Works // if (commoditiesWithoutMap != null && commoditiesWithoutMap.Count > 0) // { // foreach (string s in commoditiesWithoutMap) // errorMsg = errorMsg + "\n" + "The server does not support the commodity: " + s; // } Trace.WriteLineIf(Utils.traceSwitch.TraceVerbose, DateTime.Now.ToUniversalTime().ToString() + ": Uploader.Upload Return"); }
private void UploadWithBrowser(UploadServer server, System.Windows.Forms.WebBrowser webBrowser) { //string url = "http://pctb.ilk.org/upload.php"; //string url = ConfigurationSettings.AppSettings["HostUrl"].ToString() + "/upload.php"; string url = server.UploadUrl.ToString(); string[] files = { "marketdata.gz" }; long length = 0; string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); boundary = "SEP9242"; string navHeader = "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"; Stream memStream = new System.IO.MemoryStream(); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] finalBoundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); memStream.Write(boundarybytes, 0, boundarybytes.Length); length += boundarybytes.Length; string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\nContent-Type: application/gzip\r\n\r\n"; for (int i = 0; i < files.Length; i++) { string header = string.Format(headerTemplate, "marketdata", files[i]); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); length += headerbytes.Length; FileStream fileStream = new FileStream(appDataDir + "\\" + files[i], FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); length += bytesRead; } if (i == (files.Length - 1)) { memStream.Write(finalBoundarybytes, 0, finalBoundarybytes.Length); } else { memStream.Write(boundarybytes, 0, boundarybytes.Length); } length += boundarybytes.Length; fileStream.Close(); } memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); webBrowser.Navigate(url, "", tempBuffer, navHeader); System.Threading.Thread.Sleep(1000); }