WebFormSubmitter Class

Imports System.Web

Imports System.Net

Imports System.IO

Public Class WebFormSubmitter

    'https://stackoverflow.com/questions/793755/how-to-fill-forms-and-submit-with-webclient-in-c-sharp

    Public Function submit(ByVal address As String,

                                  ByVal values As Dictionary(Of String, String)) As String

        Dim encoding As New UTF8Encoding

        Dim postData As String = getPostData(values:=values)

        Dim data() As Byte = encoding.GetBytes(postData)

        Dim request = CType(WebRequest.Create(address), HttpWebRequest)

        request.Method = "POST"

        request.ContentType = "application/x-www-form-urlencoded"

        request.ContentLength = data.Length

        'https://stackoverflow.com/questions/10205854/error-the-remote-server-returned-an-error-401-unauthorized

        request.UseDefaultCredentials = True

        request.PreAuthenticate = True

        request.Credentials = CredentialCache.DefaultCredentials

        Dim newStream = request.GetRequestStream()

        newStream.Write(data, 0, data.Length)

        newStream.Close()

        Dim response = request.GetResponse()

        Dim responseStream = response.GetResponseStream()

        Dim responseReader = New StreamReader(responseStream)

        Return responseReader.ReadToEnd()

    End Function

    Private Function getPostData(ByVal values As Dictionary(Of String, String)) As String

        Dim postDataPairList As New List(Of String)

        For Each anEntry In values

            postDataPairList.Add(anEntry.Key & "=" & HttpUtility.UrlEncode(anEntry.Value))

        Next

        Return String.Join(separator:="&", values:=postDataPairList)

    End Function

End Class

WebFormSubmitter Example

Dim dict As New Dictionary(Of String, String)

                dict.Add("ip", Button1555.Label)

                dict.Add("cidr", "/32")

                dict.Add("send", "submit")

                dict.Add("requestor_show_to_user", Replace(Environment.UserName, Environment.UserDomainName & "\", ""))

                Dim webform As New WebFormSubmitter

                Dim response As String = webform.submit("http://cnn/it/ipblock/add-dev.asp", dict)

                Dim frmWeb As New Form2

                frmWeb.WebBrowser1.DocumentText = response

                frmWeb.Show()

.