0

im trying to get size of my direct links in richtextbox. in richtextbox exist 20 or 30 links.my code work for one link. how I get size of all link that exist in richtextbox? this is my code:

private void button1_Click(object sender, EventArgs e)
{
    HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(richTextBox1.Text);
    hwr.Method = "HEAD";
    HttpWebResponse res = (HttpWebResponse)hwr.GetResponse();
    long len = res.ContentLength;
    long a = len / 1024;
    long b = a / 1024;
    MessageBox.Show(b.ToString() + " MB");
}
4
  • By size, do you mean count?
    – crush
    Commented Jan 30, 2014 at 19:40
  • I just want to for example I have a move link that have 700mb size.now I have more than one link in richtextbox.how I get size of all links?
    – master
    Commented Jan 30, 2014 at 19:43
  • 1
    What is a move link? Why is a link 700mb in size?
    – crush
    Commented Jan 30, 2014 at 19:44
  • im sorry movie not move
    – master
    Commented Jan 30, 2014 at 19:46

1 Answer 1

0

My proposal depends on how your richTextBox.Text looks like, but if it has a defined separator - the code below should work - it will split your text into lines, then will perform your request on every line:

private void button1_Click(object sender, EventArgs e)
{
    string[] movies = richTextBox1.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
    foreach (string item in movies)
    {
      item.Trim();
      HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(item);
      hwr.Method = "HEAD";
      HttpWebResponse res = (HttpWebResponse)hwr.GetResponse();
      long len = res.ContentLength;
      long a =len/1024;
      long b = a / 1024;
      MessageBox.Show(b.ToString()+" MB");
    }
}

EDIT - I think a code would work better if it was asynchronous, it can look like this:

    List<long> Length = new List<long>();
    List<string> Movies = new List<string>();

    private async void button1_Click(object sender, EventArgs e)
    {
        await getLength(Regex.Split(richTextBox1.Text, "\r\n|\r|\n"));
        string text = "";
        for (int i = 0; i < Movies.Count; i++)
            text += Movies[i] + " Length: " + Length[i].ToString() + "\n";
        MessageBox.Show(text);
    }

    private async Task getLength(string[] movies)
    {
        Task[] tasks = new Task[movies.Length];
        for (int i = 0; i < movies.Length; i++)
        {
            Movies.Add(movies[i].Trim());
            Length.Add(0);
            tasks[i] = getOnesLength(i);
        }
        await Task.WhenAll(tasks);
    }

    private async Task getOnesLength(int index)
    {
        HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(Movies[index]);
        hwr.Method = "HEAD";
        HttpWebResponse res = (HttpWebResponse) await hwr.GetResponseAsync();
        long len = res.ContentLength;
        long a = len / 1024;
        long b = a / 1024;
        Length[index] = b;
    }

In this case your UI shouldn't be blocked and you should see a MessageBox when getting info for all links is complete. Of course this code lacks exception handling and so on but it may be a good start.

4
  • @master how your richTextBox.Text looks like? Are there separators between links?
    – Romasz
    Commented Jan 30, 2014 at 20:14
  • @master Can you debug it and check if your Text had been split correct? Or maybe try to split it with string[] movies = Regex.Split(text, "\r\n|\r|\n");
    – Romasz
    Commented Jan 30, 2014 at 20:23
  • @master I've added Trim() method - to remove spaces at the end of string. I think the problem is that splitted string is not correct. Can you set breakpoint at foreach and check how movies[] look like?
    – Romasz
    Commented Jan 30, 2014 at 20:46
  • @master I've added an asynchronous version (thought I've have not tried it), but it may be helpful for you. And don't forget to mark an answer as a solution (green tick besides) if it solved your problem.
    – Romasz
    Commented Jan 30, 2014 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.