我正在嘗試運行我的第一個 VB 程式,我正在嘗試運行 HTTP POST 請求,當我對我的帳戶詳細資訊進行硬編碼但想使用文本框作為用戶名和密碼輸入時,我將它功能化,我正在學習去,歡迎任何優化和改進的輸入,我計劃添加互聯網檢查(ping 或類似)& 僅在離線時嘗試登錄,但它是 WIP,關于我試圖實作的更多資訊在這里 https://github.com /aidanmacgregor/BTWi-fi_Autologin-Shell_Script-MACRODROID-WISPr-HTTP_POST-HTTP_GET-OpenWRT
我需要編輯 [email protected] 和 PASSWORD 部分,但保留 username= 和 &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
uj5u.com熱心網友回復:
好的,所以我仍然不能 100% 確定我們在這里得到了完整的圖片,特別是在你提到的 Null 物件參考例外之后,如果你真的用相關細節更新了你的問題,仍然會非常有幫助。包括您在評論中發布的解決方案,您有一些選擇。我寫這個答案是因為我不覺得繼續通過評論提供建議會取得更多成就。
選項 1 - 使用“$”字串插值運算子(但是這是拼寫的)
Dim postData As String = $"username={txtEmail.Text}&password={txtPassword.Text}"
選項 2 - 使用 String.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
假設沒有其他潛在問題,這些選項中的任何一個(順便說一句)都將提供所需的憑據字串
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490858.html
上一篇:asp.net(VB)使用CodeFile且無命名空間的奇怪雙影像上傳
下一篇:我如何讓這個文本框實時更新
