首页 > 解决方案 > VB.net HTTP POST 请求

问题描述

我意识到关于这个主题有几个类似的帖子,但我还没有找到解决方案。我想通过 POST 请求发送下面字段中的数据。

带有搜索表单的图像

这是我到目前为止所做的代码。

Dim result = ""
        Dim URL = "http://derogations/index.php"

        'Create a New WebClient instance.
        Dim myWebClient = New WebClient()
        Dim LeMessage As String
        LeMessage = "filtre_statut=*"
        LeMessage &= "&filtre_client=*"
        LeMessage &= "&filtre_type=*"
        LeMessage &= "&filtre_demandeur=*"
        LeMessage &= "&filtre_validateur=*"
        LeMessage &= "&filtre_recherche=RATNOV0025AB"
        myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

        'Display the headers in the request
        MsgBox(myWebClient.Headers.ToString())

        'Apply ASCII Encoding to obtain the string as a byte array.
        Dim byteArray() As Byte = Encoding.ASCII.GetBytes(LeMessage)

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

        ServicePointManager.ServerCertificateValidationCallback += New System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate)

        Dim req = CType(WebRequest.Create(URL), HttpWebRequest)
        req.AllowWriteStreamBuffering = True
        req.ContentType = "application/x-www-form-urlencoded"
        req.Method = "POST"
        'req.ContentLength = bytes.Length;
        req.KeepAlive = False
        req.ProtocolVersion = HttpVersion.Version10
        'req.Timeout = -1;

        Try
            Using writer As New StreamWriter(req.GetRequestStream(), Encoding.ASCII)
                writer.WriteLine(byteArray)
            End Using
            Using resp As WebResponse = req.GetResponse()
                Using sr As StreamReader = New StreamReader(resp.GetResponseStream())
                    result = sr.ReadToEnd()
                End Using
            End Using

        Catch ex As Exception
            result = MsgBox(ex.ToString)
        End Try

标签: vb.net

解决方案


对于那些对结果感兴趣的人。这是解决方案的主题。

VB.net 打开一个填写了表单域的网页


推荐阅读