Link to home
Start Free TrialLog in
Avatar of Aetia
Aetia

asked on

Repeat a command

Lets say that I have button "Run" and if I click it then it writes something to "textbox1".

How can I do so that it would repeat this commad number of times defined in "textbox2", every time deleting previously writed information.
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

you can do this in a FOR loop with its upper bound set to the value in the textbox2
something close to this:

Dim Limit As Int32 = Convert.ToInt32(TextBox2.Text)
Dim i As Int32

For i = 1 To Limit
            TextBox1.Text = "The Value now is " + Limit.ToString()
Next
Avatar of Dirk Haest
I guess you want to loop as long as the value in textbox2 isn't reached.
I tried this. In my textbox2 was "2" as value entered

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Short
        For i = 1 To CShort(TextBox2.Text)
            MsgBox("TextBox1.Text = " & i)
            TextBox1.Text = i
        Next i
    End Sub
Avatar of Aetia
Aetia

ASKER

Ok! Almost that what I am looking for...

How can I do it without msgbox? I meand it would coun times on textbox1 or say atleast after every repeat completed etc?
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Short
        For i = 1 To CShort(TextBox2.Text)
            TextBox1.Text = i
        Next i
    End Sub
did you try my code?
Avatar of Aetia

ASKER

dhaest, Dhaest I've tried it...

Cast from string "TextBox2" to type 'Short' is not valid.
What value do you have in textbox2 ?
Avatar of Aetia

ASKER

OK! Maybe I can make it more simple for you... Here is what I want to make loop. When I click on this button it downloads website and shows me source code. What I want to do is to loop this command so it would generate number of hits to my website.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cmdCancel.Enabled = True

        Dim hgf As Short
        For hgf = 1 To CShort(TextBox2.Text)
            MsgBox("Label13.Text = " & hgf)
            Label13.Text = hgf
        Next hgf

        wr = New clsWebRequest(txtRequest.Text)
        If Not wr.RequestObj Is Nothing Then
            '- Set The Status Callback Address
            wr.StatusCallBack = AddressOf ReqCallBack

            '- Use Smart Auto Redirection (Cookie Support)
            wr.UseSmartRedirection = True

            '- Set Up The Request Headers
            If txtReferer.Text <> "" Then wr.RequestObj.Referer = txtReferer.Text
            If txtUserAgent.Text <> "" Then wr.RequestObj.UserAgent = txtUserAgent.Text
            If txtAccept.Text <> "" Then wr.RequestObj.Accept = txtAccept.Text

            Select Case True
                Case rbConnectionOther.Checked
                    wr.RequestObj.KeepAlive = False
                    If txtConnection.Text <> "" Then wr.RequestObj.Connection = txtConnection.Text

                Case rbConnectionClose.Checked
                    wr.RequestObj.KeepAlive = False

                Case rbConnectionKeepAlive.Checked
                    wr.RequestObj.KeepAlive = True
            End Select

            '- Add The Custom Headers
            Dim i As Integer
            For i = 0 To lvHeaders.Items.Count - 1
                wr.RequestObj.Headers.Add(lvHeaders.Items(i).Text, lvHeaders.Items(i).SubItems(1).Text)
            Next

            '- Setup The Proxy Info
            If chkUseProxy.Checked Then
                wr.RequestObj.Proxy = New System.Net.WebProxy(txtProxyAddress.Text, CInt(txtProxyPort.Text))
                If txtProxyDomain.Text <> "" Then
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text, txtProxyDomain.Text)
                Else
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text)
                End If
            End If

            '- Add The Data To Post
            If chkPost.Checked Then
                For i = 0 To lvPost.Items.Count - 1
                    wr.AddPostData(lvPost.Items(i).Text, lvPost.Items(i).SubItems(1).Text)
                Next
            End If

            '- Setup The Proxy Info
            If chkAuth.Checked Then
                wr.RequestObj.PreAuthenticate = True
                wr.RequestObj.Credentials = New Net.NetworkCredential(txtAuthUsername.Text, txtAuthPassword.Text)
            End If

            '- Setup For Resuming
            wr.DoResume = chkResume.Checked
            If wr.DoResume Then wr.ResumeData = txtResumeData.Text

            '- Make The Request
            wr.ProcessRequest()
        End If
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Limit As Int32 = Convert.ToInt32(TextBox2.Text)
Dim i As Int32

For i = 1 To Limit
            TextBox1.Text = Limit.ToString()
Next

end sub
Avatar of Aetia

ASKER

Desp, look my previus post, how can I make this wr.ProcessRequest() to loop?

As far as I've tried it says I cant run it if previous call isn't ended...

Any solution for that?
clsWebRequest I presume is a custom class since I couldn't find any documentation on it.

If so is there an event in it to call back to your app when it is finished?

If there isn't an event and you created the class then add one.

If you didn't create the class do you have access to modify it.

Anyway if you catch the the (ProcessRequestFinished or whatever) event you can then run the next loop since you know the first ProcessRequest is finished.

Your module might look like this. Except you will need to replace ProcessRequestFinished with whatever the actual event is called.

Dim LoopsLeft as int
Dim WR as clsWebRequest

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                             Handles Button1.Click
  LoopsLeft = TextBox1.text
  StartProcessRequest ()

End Sub

PrivateSub StartProcessRequest ()
        Dim hgf As Short
        For hgf = 1 To CShort(TextBox2.Text)
            MsgBox("Label13.Text = " & hgf)
            Label13.Text = hgf
        Next hgf

        wr = New clsWebRequest(txtRequest.Text)
        If Not wr.RequestObj Is Nothing Then
            '- Set The Status Callback Address
            wr.StatusCallBack = AddressOf ReqCallBack

            '- Use Smart Auto Redirection (Cookie Support)
            wr.UseSmartRedirection = True

            '- Set Up The Request Headers
            If txtReferer.Text <> "" Then wr.RequestObj.Referer = txtReferer.Text
            If txtUserAgent.Text <> "" Then wr.RequestObj.UserAgent = txtUserAgent.Text
            If txtAccept.Text <> "" Then wr.RequestObj.Accept = txtAccept.Text

            Select Case True
                Case rbConnectionOther.Checked
                    wr.RequestObj.KeepAlive = False
                    If txtConnection.Text <> "" Then wr.RequestObj.Connection = txtConnection.Text

                Case rbConnectionClose.Checked
                    wr.RequestObj.KeepAlive = False

                Case rbConnectionKeepAlive.Checked
                    wr.RequestObj.KeepAlive = True
            End Select

            '- Add The Custom Headers
            Dim i As Integer
            For i = 0 To lvHeaders.Items.Count - 1
                wr.RequestObj.Headers.Add(lvHeaders.Items(i).Text, lvHeaders.Items(i).SubItems(1).Text)
            Next

            '- Setup The Proxy Info
            If chkUseProxy.Checked Then
                wr.RequestObj.Proxy = New System.Net.WebProxy(txtProxyAddress.Text, CInt(txtProxyPort.Text))
                If txtProxyDomain.Text <> "" Then
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text, txtProxyDomain.Text)
                Else
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text)
                End If
            End If

            '- Add The Data To Post
            If chkPost.Checked Then
                For i = 0 To lvPost.Items.Count - 1
                    wr.AddPostData(lvPost.Items(i).Text, lvPost.Items(i).SubItems(1).Text)
                Next
            End If

            '- Setup The Proxy Info
            If chkAuth.Checked Then
                wr.RequestObj.PreAuthenticate = True
                wr.RequestObj.Credentials = New Net.NetworkCredential(txtAuthUsername.Text, txtAuthPassword.Text)
            End If

            '- Setup For Resuming
            wr.DoResume = chkResume.Checked
            If wr.DoResume Then wr.ResumeData = txtResumeData.Text

            '- Make The Request
            wr.ProcessRequest()
        End If
End sub
Private Function WR_ProcessRequestFinished () as long _
                                            Handles WR.ProcessRequestFinished
If LoopsLeft > 0 then
   StartProcessRequest
end if
LoopsLeft - 1



End Function
Avatar of Aetia

ASKER

I think I've found this what you call "ProcessRequestFinished":

Private Shared Sub ResponseCallback(ByVal AsyncResult As IAsyncResult)
        Dim ReqState As RequestState = CType(AsyncResult.AsyncState, RequestState)

        If bCancel Then
            ReqState.Request.Abort()
            If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_REQUESTABORTED, 0, 0, 0)
            ReqThread.Abort()
            Exit Sub
        End If

        Try
            ReqState.Response = ReqState.Request.EndGetResponse(AsyncResult)
            If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_HEADERRECEIVED, 0, IIf(bResume, ReqState.Response.ContentLength + ReqState.lResumeStart, ReqState.Response.ContentLength), 0)

            '- Smart Redirection
            If bUseSmartRedirection Then
                Select Case ReqState.Response.StatusCode
                    Case HttpStatusCode.Found, HttpStatusCode.Moved, HttpStatusCode.MovedPermanently

                        '- Invoke The Status Callback
                        If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_HEADERRECEIVED, 0, ReqState.Response.ContentLength, 0)

                        '- Change URL To Absolute If Relative
                        Dim i As Integer
                        Dim sNewUrl As String = ReqState.Response.Headers.Item("Location")
                        If Not IsValidUrl(sNewUrl) Then
                            sNewUrl = ReqState.Response.ResponseUri.Scheme & ReqState.Response.ResponseUri.SchemeDelimiter & ReqState.Response.ResponseUri.Host
                            sNewUrl = sNewUrl & ReqState.Request.RequestUri.LocalPath
                            sNewUrl = sNewUrl & ReqState.Response.Headers.Item("Location")
                        End If

                        '- Create New Request With The New Address
                        Dim NewReq As HttpWebRequest = HttpWebRequest.Create(sNewUrl)
                        NewReq.Proxy = ReqState.Request.Proxy
                        NewReq.UserAgent = ReqState.Request.UserAgent
                        NewReq.PreAuthenticate = ReqState.Request.PreAuthenticate
                        NewReq.Credentials = ReqState.Request.Credentials
                        NewReq.Referer = ReqState.Request.RequestUri.ToString

                        '- Include Any Cookies Received
                        Dim sCookie() As String
                        sCookie = ReqState.Response.Headers.GetValues("Set-Cookie")
                        If Not sCookie Is Nothing Then
                            For i = 0 To UBound(sCookie)
                                NewReq.Headers.Add("Cookie: " & sCookie(i))
                            Next i
                        End If
                        Erase sCookie
                        ReqState.Request = NewReq

                        '- Request The New Address
                        If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_REQUESTINGHEADER, 0, 0, 0)
                        AsyncResult = CType(ReqState.Request.BeginGetResponse(AddressOf ResponseCallback, ReqState), IAsyncResult)
                        Exit Sub
                End Select
            End If

            '- Setup OutputFile If Needed
            If sOutputFile <> "" Then
                If bResume Then
                    ReqState.ByteLen = ReqState.lResumeStart
                    ReqState.OutputFileStream = New FileStream(sOutputFile, FileMode.Append, FileAccess.Write, FileShare.None)
                Else
                    ReqState.OutputFileStream = New FileStream(sOutputFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
                End If
                ReqState.Data.Append(sOutputFile)
            Else
                ReqState.OutputFileStream = Nothing
                If bResume Then
                    ReqState.Data.Append(sResumeData)
                    ReqState.ByteLen = sResumeData.Length
                End If
            End If

            ReqState.ResponseStream = ReqState.Response.GetResponseStream()
            Dim aResult As IAsyncResult = ReqState.ResponseStream.BeginRead(ReqState.Buffer, 0, BUFFER_SIZE, AddressOf ReadCallBack, ReqState)

        Catch we As WebException
            '- Make The Response Available For Analizing Afterwards
            ReqState.Response = we.Response

            Dim iErrNum As Integer = CType(we.Response, HttpWebResponse).StatusCode

            '- Notify Owner Of Failed Request
            If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_REQUESTFAILED, 0, 0, iErrNum)

            '- End This Thread
            ReqThread.Abort()
        End Try
    End Sub
It seems that you have the class so you can modify it.

I don't think that is quite what I was looking for.

Since you can modify the class directly, at the top of the class declare an event

    Public Event ProcessRequestFinished ()

Look for a sub in the clsWebRequest class named ProcessRequest
Once you find this sub go to the end of the sub and for the last line put

    RaiseEvent ProcessRequestFinished()

this will start running the code in your module that handles this event.

Private Function WR_ProcessRequestFinished () as long _
                                            Handles WR.ProcessRequestFinished
   If LoopsLeft > 0 then
     StartProcessRequest
   end if
   LoopsLeft - 1

End Function

Then you should be golden.

Corey
Avatar of Aetia

ASKER

I am newbie and started learning vb.net. Everything you said is currently mishmash for me... What I think that you could download my solution an see it yourself.

Download it here:
http://www.dosch3d.ee/HttpWebRequest.rar

Maybe you could give me full parts of codes so I could get this thing working and later examine it...

What I want to develop of this thing would be "traffic bump" that would generate numerous hits to some webpage. That's why I need it to loop it set number of times.
ASKER CERTIFIED SOLUTION
Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial