-1

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 USERNAME@btinternet.com and PASSWORD parts but leave the username= and &password=

Dim postData As String = "username=USERNAME@btinternet.com&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 = "username=USERNAME@btinternet.com&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
braX
  • 11,506
  • 5
  • 20
  • 33
  • Well, it's not much more difficult than dropping a couple textbox controls on your form then getting the values to build your login string. All in all, you probably need to rework you're question some to explain what part of that you're having trouble with though. Basically You can access the text or value property on textboxes to get the data, then use string formatting to build the credential string. If you're after a code review of working code you are better off posting here https://codereview.stackexchange.com/ – Hursey Jun 06 '22 at 23:21
  • This is first time using VB so bear with my lack of knowledge, i need to edit the line HTTP POST response data (username=USERNAME@btinternet.com&password=PASSWORD) – Aidan Macgregor Jun 06 '22 at 23:40
  • 1
    Rant aside, something like Dim postData As String = $"username={txtUser.Value}&password={txtPassword.Value}" Should get you moving. String.Format(), StringBuilder(), string Interpolated Strings (Per my example) and finally "&" to concatenate strings are all different tools you could read up on – Hursey Jun 06 '22 at 23:48
  • but the issue is i cant use the text box input in that variable as its in double quotes, i have text boxes set up named txtEmail and txtPassword, ive tried this, it dosnt work bust should help illustrate my goal ---- Dim postData As String = "username="txtEmail"&password="txtPassword – Aidan Macgregor Jun 06 '22 at 23:50
  • i have been doing research, non stop for about a week, first time ive asked any help whatsoever, ive read about and tried to cencetrate stings and couldnt get it to work\ – Aidan Macgregor Jun 06 '22 at 23:52
  • First off, this isn't going to work anyway username="txtEmail" txtEmail is a Textbox control, not a string. You need edit your question, post your actual attempt at building the creditable string and strip out all the irrelevant code that has nothing to do with the actual problem you're having. This isn't being a d**khead, this trying to get you to post a question that can be answered – Hursey Jun 06 '22 at 23:58
  • You're not trying to modify any parts of a string. What you're trying to do is build a string from multiple parts. I just checked [here](https://www.homeandlearn.co.uk/NET/vbNet.html), which is a popular tutorial for beginners, and it first shows an example of concatenating strings in part 13, entitled "String Variables". If you're doing the stuff you are in this code, you should already know how to concatenate strings. It's not for us to teach you the absolute basics of the language. In case you think I'm being mean too, how exactly do you think we should tell you "no"? – John Jun 07 '22 at 01:29
  • ive been using the above site & is the whole point of forums & community not to help others out? i get it you don't want to hold everyone's hand, but just some pointers in the right direction can be so helpful & hearing explanations As you have both done & it is appreciated, tried the following but ran into an issue "An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object." – Aidan Macgregor Jun 07 '22 at 02:31
  • Dim postPreEmail As String = "username=" Dim postEmail As String = txtEmail.Text Dim postPrePassword As String = "&password=" Dim postPassword As String = txtPassword.Text Dim postData As String = postPreEmail & postEmail & postPrePassword & postPassword – Aidan Macgregor Jun 07 '22 at 02:31

1 Answers1

0

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

Hursey
  • 541
  • 2
  • 8
  • 17
  • Thanks, I had tried this i think before, realised was on older version of VB (2010) upgraded to Studio 2022 & works, The Solution i used was "Dim postData As String = $"username={txtEmail.Text}&password={txtPassword.Text}"" – Aidan Macgregor Jun 13 '22 at 14:25
  • If that was the case, either option 2 or 3 would of worked – Hursey Jun 13 '22 at 19:54