为什么不在VB.NET中发送HTTP POST请求

时间:2017-09-04 01:14:12

标签: vb.net

我查了一下,发现了一些通过VB.NET程序发送HTTP POST请求的不同方法,但是没有一种方法适合我。 这是我正在尝试的当前版本,有人能告诉我它为什么不起作用吗?

Imports System.Net
Imports System.Text
Imports System.IO

Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim postData As String = "exampleid=454621696&text=yesitworked"
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)

        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://website.com/comments/post/"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.Referer = "https://website.com"
        postReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        postReq.ContentLength = byteData.Length

        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()
        Dim postresponse As HttpWebResponse

        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
        Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

        Dim thepage As String = postreqreader.ReadToEnd

        RichTextBox1.Text = thepage

    End Sub
End Class

从我所看到的情况来看,我认为它并不是在发送请求,因为它在尝试获得响应时会发出错误,但就像我说的那样,我认为这是因为请求甚至没有运行。

由于

0 个答案:

没有答案