Visit Each URL In A ListBox

v7web

Registered Member
Joined
Jul 15, 2010
Messages
64
Reaction score
4
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
 
for each itm as string in listbox1.items
create a webrequest, do a POST for the signup
next
 
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!
 
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!

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?
 
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?

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?
 
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));
            }
 
If you still need an example then PM me tonight and I'll do it for you when I get home.
 
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
 
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

as far as i know there are no books.

have to figure it out as you go using google and your imagination.
 
as far as i know there are no books.

have to figure it out as you go using google and your imagination.

Yeah man, this bugs me a lot. they should really start to include more information on httpwebrequest and other web related functions in tutorials!
 
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.
 
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
 
It says this Unable to cast object of type 'System.String' to type 'System.Text.RegularExpressions.Match'.
 
Back
Top