1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Visit Each URL In A ListBox

Discussion in 'Visual Basic .NET' started by v7web, Feb 14, 2011.

  1. v7web

    v7web Registered Member

    Joined:
    Jul 15, 2010
    Messages:
    51
    Likes Received:
    3
    Hi,
    I need to visit each url in a list (ListBox) to activate a sign up.
    I would like to do this without using the webbrowser control.

    Any advice?

    Thanks
     
  2. w84it

    w84it Regular Member

    Joined:
    Jun 18, 2009
    Messages:
    442
    Likes Received:
    752
    for each itm as string in listbox1.items
    create a webrequest, do a POST for the signup
    next
     
  3. inaga

    inaga Newbie

    Joined:
    Feb 4, 2010
    Messages:
    12
    Likes Received:
    3
    Code:
            ' For a GET request
            Dim webC As New WebClient
            Dim strResponse As String = ""
            For Each strUrl As String In ListBox1.Items
                strResponse = webC.DownloadString(strUrl)
                Do Until webC.IsBusy = False
                    Application.DoEvents()
                Loop
                webC.Dispose()
            Next
    
    Code:
            ' For a POST request
            Dim webC As New WebClient
            Dim strResponse As String = ""
            Dim strPostData As String = "your post data here"
            For Each strUrl As String In ListBox1.Items
                strResponse = webC.UploadString(strUrl, strPostData)
                Do Until webC.IsBusy = False
                    Application.DoEvents()
                Loop
                webC.Dispose()
            Next
    
    Hope that helps!
     
  4. v7web

    v7web Registered Member

    Joined:
    Jul 15, 2010
    Messages:
    51
    Likes Received:
    3
    Thanks inaga,
    However when the list of urls is bigger than1 I get the following error:

    InvalidCastException was unhandled
    Conversion from type 'Match' to type 'String' is not valid.

    Any ideas?
     
  5. inaga

    inaga Newbie

    Joined:
    Feb 4, 2010
    Messages:
    12
    Likes Received:
    3
    Try this:

    Code:
            Dim webC As New WebClient
            Dim strResponse As String = ""
            Dim strUrl As String = ""
            For Each itemMatch As Match In ListBox1.Items
                strUrl = itemMatch.ToString
                strResponse = webC.DownloadString(strUrl)
                Do Until webC.IsBusy = False
                    Application.DoEvents()
                Loop
                webC.Dispose()
            Next
    
    Will something like that work?
    If not..
    Does your list contain just regex matches or a mix of different types of items?
     
  6. Senotaru

    Senotaru Registered Member

    Joined:
    Jan 17, 2011
    Messages:
    67
    Likes Received:
    11
    I wrote this up for you really quick, but it's in C#. It should be simple enough to switch over to VB. It currently only gets the site, and doesn't post. If you want it to post, you're going to need to write your post string to the request stream after you cast it into a byte array.

    Code:
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                listBox1.Items.Add(textBox1.Text);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                foreach (String item in listBox1.Items)
                {
                    MessageBox.Show(ViewWebsite.Connect(item));
                }
            }
        }
    
        public class ViewWebsite
        {
            public static string Connect(String url)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //Set Request Paramaters here.
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader responseStream = new StreamReader(response.GetResponseStream());
                return responseStream.ReadToEnd();
            }
        }
    
    edit: If you're adding regex values to your listbox, switch out the following code:
    Code:
                foreach (String item in listBox1.Items)
                {
                    MessageBox.Show(ViewWebsite.Connect(item));
                }
    for:
    Code:
    
                foreach (Match item in listBox1.Items)
                {
                    MessageBox.Show(ViewWebsite.Connect(item.Value));
                }
    
     
  7. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    If you still need an example then PM me tonight and I'll do it for you when I get home.
     
  8. cbnoob

    cbnoob Senior Member

    Joined:
    Sep 27, 2010
    Messages:
    967
    Likes Received:
    455
    I don't want to hijack this thread but what book or which place for me learn stuffs like this (writing application that dealing with websites in vb.net). I have read few books about vb.net for beginner but they only show how to build desktop applications, not handling with websites.

    Thanks a lot
     
  9. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    as far as i know there are no books.

    have to figure it out as you go using google and your imagination.
     
    • Thanks Thanks x 1
  10. Coz im Gangsta

    Coz im Gangsta Junior Member

    Joined:
    Apr 19, 2009
    Messages:
    107
    Likes Received:
    77
    Location:
    Youtube Bot Heaven
    Yeah man, this bugs me a lot. they should really start to include more information on httpwebrequest and other web related functions in tutorials!
     
  11. yashachan

    yashachan Newbie

    Joined:
    Jan 11, 2012
    Messages:
    7
    Likes Received:
    0
    for each itm in listbox1.items
    process.start(itm)
    next
     
  12. Ampix0

    Ampix0 Power Member

    Joined:
    Jan 10, 2012
    Messages:
    525
    Likes Received:
    60
    Home Page:
    Alright I am a C# coder and I have a similar issue. What If I don't use loops, I need to use timers to slow down the program(with variable intervals). I pretty much need to use timers for what I am doing.

    I need to get one item out at a time and add to a string
    so like

    String current = listbox1.items(i)

    however that doesn't work but I need that basically.
     
  13. polas

    polas Newbie

    Joined:
    Jul 8, 2009
    Messages:
    11
    Likes Received:
    0
    Can you tell me where to put url address like to get links in listbox cuz i do not see where ?

    I converted your code.

    Code:
    Public Sub New()
    	InitializeComponent()
    End Sub
    
    Private Sub button1_Click(sender As Object, e As EventArgs)
    	listBox1.Items.Add(textBox1.Text)
    End Sub
    
    Private Sub button2_Click(sender As Object, e As EventArgs)
    	For Each item As [String] In listBox1.Items
    		MessageBox.Show(ViewWebsite.Connect(item))
    	Next
    End Sub
    
    Public Class ViewWebsite
    	Public Shared Function Connect(url As [String]) As String
    		Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    		'Set Request Paramaters here.
    
    		Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    		Dim responseStream As New StreamReader(response.GetResponseStream())
    		Return responseStream.ReadToEnd()
    	End Function
    End Class
    
    For Each item As [String] In listBox1.Items
    	MessageBox.Show(ViewWebsite.Connect(item))
    Next
    
    For Each item As Match In listBox1.Items
    	MessageBox.Show(ViewWebsite.Connect(item.Value))
    Next
     
  14. polas

    polas Newbie

    Joined:
    Jul 8, 2009
    Messages:
    11
    Likes Received:
    0
    It says this Unable to cast object of type 'System.String' to type 'System.Text.RegularExpressions.Match'.