-1

I want to post a form in VB.NET with values username, password, and account_number programmatically in the background and then display the landing page in a VB WebBrowser.

I tried that with no success.

Code:

Protected Sub PostTo(url As String, postData As String)
    Dim myWebRequest As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
    myWebRequest.Method = "POST"
    Dim byteArray As Byte() = System.Text.Encoding.[Default].GetBytes(postData)
    myWebRequest.ContentType = "application/x-www-form-urlencoded"
    myWebRequest.ContentLength = byteArray.Length

    Dim dataStream As System.IO.Stream = myWebRequest.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

    dataStream = myWebResponse.GetResponseStream()
    ''

    Dim reader As New System.IO.StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    WebBrowser1.DocumentText = "0"
    WebBrowser1.Document.OpenNew(True)
    WebBrowser1.Document.Write(responseFromServer.ToString)
    WebBrowser1.Refresh()
    ''MsgBox(responseFromServer)

    reader.Close()
    dataStream.Close()
    myWebResponse.Close()

End Sub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Ismail
  • 700
  • 4
  • 9
  • 22
  • Note that [HtmlDocument.OpenNew(True)](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.htmldocument.opennew) **returns** a new HtmlDocument where you can the Write a new HTML page. Using a WebBrowser, you just need to find (`GetElementsByTagName`) the Button element with Attribute `Submit` and `InvokeMember("click")`. – Jimi Mar 13 '19 at 20:34
  • I don't want to display the login page on the WebBrowser. I want to be able to login in the background. I'm not even sure if its even possible. Thanks for the reply though Jimi. I want to click on a button, process the login in the background and show the landing page in the WebBrowser. I've been trying to get an answer. I can't find anything. Unless the only way to do so, is go to that page and invoke the submit button after populating the fields. – Mark Ismail Mar 13 '19 at 20:44
  • I'm referring to the WebBrowser class, not the Control. You can instantiate a new WebBrowser with `Dim browser as WebBrowser = new WebBrowser()`, navigate to an URI, wait for the Document to load, then submit the Login informations. Nothing will be shown anywhere. All operations are performed in the background. If you need to show the landing page, you can also use a WebBrowser Control and perform the Login procedure when the Control is hidden. You can also use HttpWebRequest or HttpClient. Much more complicated, though. – Jimi Mar 13 '19 at 20:48
  • I'm going to try that. I really appreciate it Jimi. – Mark Ismail Mar 13 '19 at 20:55
  • A sample procedure in VB.Net here: [Auto Website Login: SetAttribute not working](https://stackoverflow.com/a/54030367/7444103). – Jimi Mar 13 '19 at 20:57

1 Answers1

0

I finally found it. The good news is VB WebBrowser is able to navigate to a specific URL while posting data.

PostData Parameter in WebBrowser Control

Sub Command1_Click()
    Dim URL As String
    Dim Flags As Long
    Dim TargetFrame As String

    Dim Headers As String

    URL = "http://YourServer" ' A URL that will accept a POST
    Flags = 0
    TargetFrame = ""

    Dim postData1 = "UserName=" & username & "&Password=" & password
    Dim dataBytes As Byte() = UTF8Encoding.UTF8.GetBytes(postData1)

    WebBrowser1.Navigate(URL, String.Empty, dataBytes, "Content-Type: application/x-www-form-urlencoded")
End Sub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Ismail
  • 700
  • 4
  • 9
  • 22