如何从文本框输入或其他变量(Visual Basic)中修改变量内的字符串的两个部分
我正在尝试运行我的第一个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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我仍然不是100%确定我们在这里有完整的图片,尤其是在您提到的null对象参考异常之后,如果您实际上向您提出了相关详细信息的询问,仍然非常有帮助。包括您在评论中发布的解决方案,您有一些选择。我写这个答案是因为我不觉得继续通过评论提供建议将取得更多成就。
选项1-使用“ $”字符串插值运算符(但是拼写为拼写)
选项2-使用字符串。Format函数
选项3-基本字符串置置,
假设没有其他基础问题,则这些选项中没有其他任何一个(有更多的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)
Option 2 - Using the String.Format Function
Option 3 - Basic String concatenation
Assuming no other underlying issues any one of these options (There are plenty more btw) will provide the required credential string