如何从文本框输入或其他变量(Visual Basic)中修改变量内的字符串的两个部分

发布于 2025-02-04 18:44:33 字数 7110 浏览 2 评论 0 原文

我正在尝试运行我的第一个VB程序,我正在尝试运行HTTP POST请求,当我对帐户详细信息进行编码时,我将其具有funcional,但想将文本框用作用户名&密码输入,我去学习以及任何输入以进行优化&欢迎改进,我计划添加Internet支票(ping或类似)&仅在离线时尝试登录,但它是一个WIP,更多有关我试图获得的信息的更多信息

我需要编辑 [email  prected] 和密码零件,但请留下用户名= and& password =

Dim postData As String = "[email protected]&password=PASSWORD"

完整代码

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2025-02-11 18:44:33

好的,所以我仍然不是100%确定我们在这里有完整的图片,尤其是在您提到的null对象参考异常之后,如果您实际上向您提出了相关详细信息的询问,仍然非常有帮助。包括您在评论中发布的解决方案,您有一些选择。我写这个答案是因为我不觉得继续通过评论提供建议将取得更多成就。

选项1-使用“ $”字符串插值运算符(但是拼写为拼写)

 Dim postData As String = $"username={txtEmail.Text}&password={txtPassword.Text}"

选项2-使用字符串。Format函数

 Dim postData as String = String.Format("username={0}&password={1}",txtEmail.Text, txtPassword.Text)

选项3-基本字符串置置,

 Dim postData As String = "username=" & txtEmail.Text & "&password=" & txtPassword.Text

假设没有其他基础问题,则这些选项中没有其他任何一个(有更多的btw)提供所需的凭证字符串

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 = 
quot;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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文