I'm trying to get my first VB Program running, i am attempting to run a HTTP POST request, i have it funcional when i hardcode my account details but would like to use the text box as username & password inputs, Im learning as i go and any input for optimisations & improvements are welcome, i plan to add internet check (ping or similar) & only attempt login when offline but its a WIP, More info on what im trying to acheive is here https://github.com/aidanmacgregor/BTWi-fi_Autologin-Shell_Script-MACRODROID-WISPr-HTTP_POST-HTTP_GET-OpenWRT
i need to edit the [email protected] and PASSWORD parts but leave the username= and &password=
Dim postData As String = "[email protected]&password=PASSWORD"
Full Code For Refrence
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Dim logincookie As CookieContainer
Dim postEmail As String = txtEmail.Text
Dim postPassword As String = txtPassword.Text
Dim postData As String = "[email protected]&password=PASSWORD"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim comboSource As New Dictionary(Of String, String)()
comboSource.Add("1", "BT Home Broadband")
comboSource.Add("2", "BT Buisness Broadband")
comboSource.Add("3", "BT Wi-Fi")
comboAcctype.DataSource = New BindingSource(comboSource, Nothing)
comboAcctype.DisplayMember = "Value"
comboAcctype.ValueMember = "Key"
End Sub
Private Sub butStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butStart.Click
Dim keyAcctype As String = DirectCast(comboAcctype.SelectedItem, KeyValuePair(Of String, String)).Key
Dim valueAcctype As String = DirectCast(comboAcctype.SelectedItem, KeyValuePair(Of String, String)).Value
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
If keyAcctype = 1 Then
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://btwifi.com:8443/tbbLogon"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = False
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://google.com"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; RV:26.0) Gecko/20100101 Firefox/26.0"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
ElseIf keyAcctype = 2 Then
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.btwifi.com:8443/ante?partnerNetwork=btb"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = False
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://google.com"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; RV:26.0) Gecko/20100101 Firefox/26.0"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
ElseIf keyAcctype = 3 Then
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.btwifi.com:8443/ante"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = False
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://google.com"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; RV:26.0) Gecko/20100101 Firefox/26.0"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
End If
End Sub
Private Sub butStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butStop.Click
Dim postData As String = ""
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://btwifi.com:8443/accountLogoff/home?confirmed=true"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = False
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://google.com"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; RV:26.0) Gecko/20100101 Firefox/26.0"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
End Sub
End Class
CodePudding user response:
Ok, so I'm still not 100% sure we've got the full picture here, espically after the Null object reference exception you've mentioned and would still be very helpful if you actually updated you question with the relevant details. Including the solution you've posted in the comments, you've got some options. I'm writing this answer because I don't feel continuing to offer advice via the comments is going to achieve anything more.
Option 1 - Using the "$" string Interpolation operator (However that is spelt)
Dim postData As String = $"username={txtEmail.Text}&password={txtPassword.Text}"
Option 2 - Using the String.Format Function
Dim postData as String = String.Format("username={0}&password={1}",txtEmail.Text, txtPassword.Text)
Option 3 - Basic String concatenation
Dim postData As String = "username=" & txtEmail.Text & "&password=" & txtPassword.Text
Assuming no other underlying issues any one of these options (There are plenty more btw) will provide the required credential string