Posting to a URL via a form

redbrad0

Member
Joined
May 31, 2007
Messages
14
Programming Experience
Beginner
I have the below code for when someone click's the login button on my application. The problem is I need to post data to this form (username & password). Can someone help me figure out how to do it?

VB.NET:
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        labLoginStatus.Text = "Please wait while we try and log you in"
        labLoginStatus.ForeColor = Color.Blue

        '# Lets request the URL to see what information is returned
        strUrlToRequest = "http://www.domain.com/return-xml.php?type=login"
        Dim myRequest As System.Net.WebRequest = System.Net.WebRequest.Create(strUrlToRequest)
        myRequest.Method = "POST"

        Dim myResponse As System.Net.WebResponse = myRequest.GetResponse()
        Dim xmlStream As System.IO.Stream = myResponse.GetResponseStream()
        xmlDocument.Load(xmlStream)

        xmlNodeList = xmlDocument.SelectNodes("/rss/channel/Login")

        Dim tmpCurrentVersion As String = xmlDocument.SelectSingleNode("/rss/channel/Login/CurrentVersion").InnerText.ToString().Trim()
        Dim tmpCurrentVersionForce As String = xmlDocument.SelectSingleNode("/rss/channel/Login/CurrentVersionForce").InnerText.ToString().Trim()
        Dim tmpCurrentMemberID As String = xmlDocument.SelectSingleNode("/rss/channel/Login/MemberID").InnerText.ToString().Trim()

        If tmpCurrentMemberID.Equals("") Then
            labLoginStatus.Text = "Your login information failed"
            labLoginStatus.ForeColor = Color.Red
        Else
            If tmpCurrentVersion.Equals(configCurrentVersion) Or (Not tmpCurrentVersion.Equals(configCurrentVersion) And Not tmpCurrentVersionForce.Equals("1")) Then
                If Not tmpCurrentVersion.Equals(configCurrentVersion) Then
                    MessageBox.Show("You are using a outdated version of this software." & vbNewLine & "Please update your software.")
                End If

                labLoginStatus.Text = ""
                groupLoginBox.Visible = False
            Else
                MessageBox.Show("You are using a outdated version of this software." & vbNewLine & "You must download new software before you can continue.")
            End If
        End If
    End Sub

In C# I would use the following code
VB.NET:
                string tmpURL = "http://www.domain.com/return-xml.php?type=login";
                string tmpPostingForm = "username=" + MemberUsername.ToString() + "&password=" + MemberPassword;
                byte[] data = System.Text.Encoding.ASCII.GetBytes(tmpPostingForm);

                System.IO.Stream os = null;
                WebRequest request = null;

                request = WebRequest.Create(tmpURL);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
                request.ContentLength = data.Length;
                request.Timeout = 420000;

                os = request.GetRequestStream();
                os.Write(data, 0, data.Length);
                os.Close();

                System.Net.WebResponse resp = null;

                resp = request.GetResponse();

                System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
                string response = sr.ReadToEnd().Trim();

                xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);
 
Then use that code. C# code can be converted, line for line, into VB.NET:

VB.NET:
                Dim tmpURL as String = "http://www.domain.com/return-xml.php?type=login"

                Dim tmpPostingForm as String = "username=" & MemberUsername.ToString & "&password=" & MemberPassword
                Dim data as Byte() = System.Text.Encoding.ASCII.GetBytes(tmpPostingForm)

                Dim os as System.IO.Stream = Nothing
                Dim request as WebRequest = Nothing

                request = WebRequest.Create(tmpURL)
                request.ContentType = "application/x-www-form-urlencoded"
                request.Method = "POST"
                request.ContentLength = data.Length
                request.Timeout = 420000

                os = request.GetRequestStream()
                os.Write(data, 0, data.Length)
                os.Close()

                Dim resp as System.Net.WebResponse = Nothing

                resp = request.GetResponse()

                Dim sr as System.IO.StreamReader = New System.IO.StreamReader(resp.GetResponseStream)
                Dim response as string = sr.ReadToEnd.Trim()

                xmlDoc = New XmlDocument
                xmlDoc.LoadXml(response)

I just did that by hand, without a converter.

C# and VB.NET are quite literally exactly the same, though C# has no "On Error" directives, requires semicolons and brackets, and various other very minor differences. But line for line, C# can be immediately converted to VB.NET 100% of the time. It's only going the other way (converting C# to VB.NET) is trickier due to statements which are not supported in C# (On Error, With...End With, etc).
 
Back
Top