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?
In C# I would use the following code
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);