private void PictureLoadCallback(IAsyncResult result) { try { RequestState2 rs = (RequestState2)result.AsyncState; // Get the WebRequest from RequestState. WebRequest req = rs.Request; // Call EndGetResponse, which produces the WebResponse object // that came from the request issued above. WebResponse resp = req.EndGetResponse(result); if (req.RequestUri.AbsoluteUri == details.PicInlayURL) { pictureBox1.Image = Bitmap.FromStream(resp.GetResponseStream()); } else if (req.RequestUri.AbsoluteUri == details.PicLoadURL) { loadingScreen.Image = Bitmap.FromStream(resp.GetResponseStream()); } else if (req.RequestUri.AbsoluteUri == details.PicIngameURL) { ingameScreen.Image = Bitmap.FromStream(resp.GetResponseStream()); } resp.Close(); } catch (WebException we) { MessageBox.Show(we.Message, "Picture Download Error", MessageBoxButtons.OK); } }
private void button1_Click(object sender, EventArgs e) { CheckedListBox.CheckedIndexCollection selectedItems = checkedListBox1.CheckedIndices; if (selectedItems.Count > 0) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { button1.Enabled = false; filesToDownload = 0; fileDownloadCount = 0; for (int f = 0; f < selectedItems.Count; f++) { char delimiter = '/'; string[] splitWords = fileList[selectedItems[f]].Split(delimiter); string localFilePath = folderBrowserDialog1.SelectedPath + "//" + splitWords[splitWords.Length - 1]; if (File.Exists(localFilePath)) { if (MessageBox.Show(checkedListBox1.Items[selectedItems[f]] + " already exists at this location.\nOverwrite existing file?", "Confirm File Replace", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { continue; } } //spawn requests for file download filesToDownload++; if (checkedListBox1.Items[selectedItems[f]] == autoLoadComboBox.Items[autoLoadComboBox.SelectedIndex]) { autoLoadFilePath = localFilePath; } try { FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(fileList[selectedItems[f]]); webRequest.Method = WebRequestMethods.Ftp.DownloadFile; // RequestState is a custom class to pass info to the callback RequestState2 rs2 = new RequestState2(); rs2.index = selectedItems[f]; rs2.Request = webRequest; IAsyncResult result2 = webRequest.BeginGetResponse(new AsyncCallback(FileDownloadCallback), rs2); ThreadPool.RegisterWaitForSingleObject(result2.AsyncWaitHandle, new WaitOrTimerCallback(InfoseekTimeout), rs2, (30 * 1000), // 30 second timeout true ); } catch (WebException we) { MessageBox.Show(we.Message, "Connection Error", MessageBoxButtons.OK); button1.Enabled = true; } } toolStripStatusLabel1.Text = "Downloading " + filesToDownload.ToString() + " files..."; } } }
private void InfoseekTimeout(object state, bool timedOut) { if (timedOut) { RequestState2 reqState = (RequestState2)state; if (reqState != null) { reqState.Request.Abort(); } MessageBox.Show("Request timed out.", "Connection Error", MessageBoxButtons.OK); } }
private void PictureLoadCallbackTimeout(object state, bool timedOut) { if (timedOut) { RequestState2 reqState = (RequestState2)state; if (reqState != null) { reqState.Request.Abort(); } MessageBox.Show("Request timed out.", reqState.webURL, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void FileDownloadCallback(IAsyncResult result) { try { RequestState2 rs = (RequestState2)result.AsyncState; // Get the WebRequest from RequestState. WebRequest req = rs.Request; // Call EndGetResponse, which produces the WebResponse object // that came from the request issued above. WebResponse resp = req.EndGetResponse(result); Stream ftpStream = resp.GetResponseStream(); char delimiter = '/'; string[] splitWords = fileList[rs.index].Split(delimiter); FileStream outputStream = new FileStream(folderBrowserDialog1.SelectedPath + "//" + splitWords[splitWords.Length - 1], FileMode.Create); long cl = resp.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); fileDownloadCount++; if (fileDownloadCount >= filesToDownload) { fileDownloadCount = 0; toolStripStatusLabel1.Text = "Downloading complete."; AutoLoadArgs arg = new AutoLoadArgs(autoLoadFilePath); EnableDownloadButton(true); OnFileDownloadEvent(this, arg); } else { toolStripStatusLabel1.Text = "Downloaded " + fileDownloadCount.ToString() + " of " + filesToDownload.ToString() + " files..."; } resp.Close(); } catch (WebException we) { MessageBox.Show(we.Message, "File Download Error", MessageBoxButtons.OK); } }
public void ShowDetails(String infoId, String _imageLoc) { toolStripStatusLabel1.Text = "Querying infoseek..."; details = new InfoDetails(); string param = "id=" + infoId; pictureBox1.ImageLocation = _imageLoc; if (checkedListBox1.SelectedItems.Count == 0) { button1.Enabled = false; } else { button1.Enabled = true; } try { lastURL = wos_select + param; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(lastURL); webRequest.Method = "GET"; // RequestState is a custom class to pass info to the callback RequestState2 rs = new RequestState2(); rs.Request = webRequest; rs.webURL = lastURL; IAsyncResult result = webRequest.BeginGetResponse(new AsyncCallback(SearchCallback), rs); ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(InfoseekTimeout), rs, (30 * 1000), // 30 second timeout true ); this.Show(); } catch (WebException we) { MessageBox.Show(we.Message, "Connection Error", MessageBoxButtons.OK); } }
private void SearchCallback(IAsyncResult result) { try { RequestState2 rs = (RequestState2)result.AsyncState; //Don't do anything if this isn't the last web request we made if (rs.webURL != lastURL) { rs.Request.EndGetResponse(result); //rs.Request.Abort(); return; } // Get the WebRequest from RequestState. WebRequest req = rs.Request; // Call EndGetResponse, which produces the WebResponse object // that came from the request issued above. WebResponse resp = req.EndGetResponse(result); // Start reading data from the response stream. Stream responseStream = resp.GetResponseStream(); // Store the response stream in RequestState to read // the stream asynchronously. xmlReader = new XmlTextReader(responseStream); xmlDoc = new XmlDocument(); xmlDoc.Load(xmlReader); xmlReader.Close(); resp.Close(); } catch (WebException we) { MessageBox.Show(we.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } XmlNodeList memberNodes = xmlDoc.SelectNodes("//result"); foreach (XmlNode node in memberNodes) { details.ProgramName = GetNodeElement(node, "title"); details.Year = GetNodeElement(node, "year"); details.Publisher = GetNodeElement(node, "publisher"); details.ProgramType = GetNodeElement(node, "type"); details.Language = GetNodeElement(node, "language"); details.Score = GetNodeElement(node, "score"); details.Authors = GetNodeElement(node, "author"); details.Controls = GetNodeElement(node, "joysticks"); details.Protection = GetNodeElement(node, "protectionScheme"); details.Availability = GetNodeElement(node, "availability"); details.Publication = GetNodeElement(node, "publication"); details.MachineType = GetNodeElement(node, "machineType"); details.PicInlayURL = GetNodeElement(node, "picInlay"); details.PicLoadURL = GetNodeElement(node, "picLoad"); details.PicIngameURL = GetNodeElement(node, "picIngame"); } if (string.IsNullOrEmpty(details.Protection)) { details.Protection = "Various"; } UpdateLabelInfo(authorLabel, details.Authors); UpdateLabelInfo(publicationLabel, details.Publication); UpdateLabelInfo(availabilityLabel, details.Availability); UpdateLabelInfo(protectionLabel, details.Protection); string controls = ""; foreach (char c in details.Controls.ToCharArray()) { if (c == 'K') { controls += "Keyboard, "; } else if (c == '1') { controls += "IF 2 Left, "; } else if (c == '2') { controls += "IF 2 Right, "; } else if (c == 'C') { controls += "Cursor, "; } else if (c == 'R') { controls += "Redefinable, "; } } UpdateLabelInfo(controlsLabel, controls); UpdateLabelInfo(machineLabel, details.MachineType); XmlNodeList fileNodes = xmlDoc.SelectNodes("/result/downloads/file"); foreach (XmlNode node in fileNodes) { String full_link = node.SelectSingleNode("link").InnerText; char delimiter = '/'; string[] splitWords = full_link.Split(delimiter); String type = node.SelectSingleNode("type").InnerText; fileList.Add(full_link); UpdateCheckbox(checkedListBox1, " (" + type + ") " + splitWords[splitWords.Length - 1]); } XmlNodeList fileNodes2 = xmlDoc.SelectNodes("/result/otherDownloads/file"); foreach (XmlNode node in fileNodes2) { String full_link = node.SelectSingleNode("link").InnerText; char delimiter = '/'; string[] splitWords = full_link.Split(delimiter); String type = node.SelectSingleNode("type").InnerText; fileList.Add(full_link); UpdateCheckbox(checkedListBox1, " (" + type + ") " + splitWords[splitWords.Length - 1]); } toolStripStatusLabel1.Text = "Ready."; if (details.PicInlayURL != "") { try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(details.PicInlayURL); webRequest.Method = WebRequestMethods.Http.Get; webRequest.UserAgent = "Zero Emulator"; // RequestState is a custom class to pass info to the callback RequestState2 rs2 = new RequestState2(); rs2.Request = webRequest; IAsyncResult result2 = webRequest.BeginGetResponse(new AsyncCallback(PictureLoadCallback), rs2); ThreadPool.RegisterWaitForSingleObject(result2.AsyncWaitHandle, new WaitOrTimerCallback(InfoseekTimeout), rs2, (30 * 1000), // 30 second timeout true ); } catch (WebException we) { MessageBox.Show(we.Message, "Connection Error", MessageBoxButtons.OK); } } if (details.PicLoadURL != "") { try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(details.PicLoadURL); webRequest.Method = WebRequestMethods.Http.Get; webRequest.UserAgent = "Zero Emulator"; // RequestState is a custom class to pass info to the callback RequestState2 rs2 = new RequestState2(); rs2.Request = webRequest; IAsyncResult result2 = webRequest.BeginGetResponse(new AsyncCallback(PictureLoadCallback), rs2); ThreadPool.RegisterWaitForSingleObject(result2.AsyncWaitHandle, new WaitOrTimerCallback(InfoseekTimeout), rs2, (30 * 1000), // 30 second timeout true ); } catch (WebException we) { MessageBox.Show(we.Message, "Connection Error", MessageBoxButtons.OK); } } if (details.PicIngameURL != "") { try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(details.PicIngameURL); webRequest.Method = WebRequestMethods.Http.Get; webRequest.UserAgent = "Zero Emulator"; // RequestState is a custom class to pass info to the callback RequestState2 rs2 = new RequestState2(); rs2.Request = webRequest; IAsyncResult result2 = webRequest.BeginGetResponse(new AsyncCallback(PictureLoadCallback), rs2); ThreadPool.RegisterWaitForSingleObject(result2.AsyncWaitHandle, new WaitOrTimerCallback(InfoseekTimeout), rs2, (30 * 1000), // 30 second timeout true ); } catch (WebException we) { MessageBox.Show(we.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }