2

这是我使用 HttpWebRequest 自动填写 Web 表单并提交的代码。

            Dim cweb As String = "http://www.yellowpages.com/novato-ca/mip/creative-memories-consultant-senior-director-461725587/send_email?lid=171673036"
            Dim POST As String = "&email%5Bto_address%5D=recipient@email.com&email%5Bfrom_name%5D=Test Name&email%5Bfrom_address%5D=sender@mail.com&email%5Bnote%5D=Hello There"       

            Dim request As HttpWebRequest
            Dim response As HttpWebResponse

            request = CType(WebRequest.Create(cweb), HttpWebRequest)
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36"
            request.AllowAutoRedirect = True
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = POST.Length
            request.Method = "POST"
            request.KeepAlive = True

            Dim requestStream As Stream = request.GetRequestStream()
            Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
            requestStream.Write(postBytes, 0, postBytes.Length)
            requestStream.Close()

            response = CType(request.GetResponse(), HttpWebResponse)
            response.Close()

在这段代码中,我使用了 Yellowpages 的邮寄表格作为示例。是的,它确实让我填写并提交,但我想要另一种选择。除了 WebClient 之外,是否还有其他替代方案,因为我已经尝试过它能够发送 POST 请求?我已经阅读了有关 System.Net.Sockets 的主题,它能够发送 POST 请求,但我不知道从哪里开始。任何提示都将被欣然接受。我真的发现 HttpWebRequest 和 WebClient 在发送 POST 请求时有点慢。

4

1 回答 1

2

您好,您可以尝试使用这种方式

Using sendto As New Net.WebClient
    Dim param As New Specialized.NameValueCollection
    param.Add("param1", "value1")
    param.Add("param2", "value2")
    Dim response_bytes = sendto.UploadValues(yourUrl, "POST", param)
    Dim response_body = (New Text.UTF8Encoding).GetString(response_bytes)
End Using
于 2014-01-21T10:21:35.323 回答