VB.net 使用 POST 方法的 HttpWebRequest 替代方式

标签 vb.net post httpwebrequest httpwebresponse

这是我使用 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%<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dde899e0afb8beb4adb4b8b3a99db8b0bcb4b1f3beb2b0" rel="noreferrer noopener nofollow">[email protected]</a>&email%5Bfrom_name%5D=Test Name&email%5Bfrom_address%<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbfe8ff6b8aea5afaeb98ba6aaa2a7e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>&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()

在此代码中,我使用黄页的邮件表单作为示例。是的,它确实让我填写并提交,但我想要其他选择。除了我已经尝试过的 WebClient 之外,还有其他能够发送 POST 请求的替代方案吗?我已阅读有关 System.Net.Sockets 的主题,它能够发送 POST 请求,但我不知道从哪里开始。任何提示都将很乐意接受。我确实发现 HttpWebRequest 和 WebClient 在发送 POST 请求时有点慢。

最佳答案

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

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

关于VB.net 使用 POST 方法的 HttpWebRequest 替代方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18900458/

相关文章:

vb.net - 如何指定 Enumerable.Count() 而不是 List.Count?

c# - 在没有 dll 的情况下在 2 个项目之间共享代码

django - Nginx + Gunicorn POST 请求错误

c# - 应用登录网站失败

ASP.NET HttpWebRequest - 从今天开始,被 Amazon 拒绝,出现 503 异常

.net - 在正则表达式匹配中替换

asp.net - 当我在表单中提交 HTML 字符时,为什么 ASP.NET 会抛出内部服务器 (500) 错误?

python - 如何使用 python 上传包含图像的表单?

php - 为什么我的 $Post 操作不起作用

c# - 向 url 发送 xml 请求并接收返回的 xml 响应