예제 #1
0
        private void AddToListBoxButton_Click(object sender, EventArgs e)
        {
            string input = AddToURIListBoxTextBox.Text;

            if (input.Length < 3)
            {
                return;
            }


            if (Uri.IsWellFormedUriString(input, UriKind.Absolute))
            {
                Uri     convertedInput = new Uri(input);
                URIItem item           = new URIItem(convertedInput);

                listOfURIs.Add(item);
                URIListBox.Items.Add(input);
            }
            else
            {
                ConvertInputLabelToError();
            }

            AddToURIListBoxTextBox.Clear();
        }
예제 #2
0
        private static Task <string> RequestItem(URIItem item, CancellationToken cancelToken)
        {
            cancelToken.ThrowIfCancellationRequested();
            return(Task.Run(() =>
            {
                // Create a new WebRequest Object to the mentioned URL.
                WebRequest req = WebRequest.Create(item.URI);

                // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
                // And get the stream containing content returned by the server.
                try
                {
                    using (WebResponse resp = req.GetResponse())
                        using (Stream streamResp = resp.GetResponseStream())
                            using (StreamReader reader = new StreamReader(streamResp))
                                // Read and return
                                return reader.ReadToEnd();
                }
                catch (Exception e)
                {
                    if (e is OperationCanceledException)
                    {
                        return "Operation cancelled.";
                    }
                    else
                    {
                        return e.Message;
                    }
                }
            }));
        }