如何使用HTTPWebRequest发送Http Post请求的正文

时间:2018-12-24 16:43:08

标签: vb.net system.net.httpwebrequest

我只想在特定URL中以x-www-form-urlencoded格式发布正文数据。我进行了很多搜索,但只发现了如何在VB.NET的HTTP请求中发送参数。

这是我的代码,但是当我看到输出时,它只是发送post参数:

Try

    Dim TextBox10 = "12637"
    Dim request As WebRequest = WebRequest.Create("https://ptsv2.com/t/delh1-1545643919")
    request.Method = "POST"

    Dim postbody As String = "employee_id=" + TextBox1.Text + "&company_code=" + TextBox2.Text + "&reg_key=" + TextBox3.Text + ""
    Dim requestBody As String = "FormFieldName=Hello+World!"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postbody)
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim response As WebResponse = request.GetResponse()
    Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
    dataStream = response.GetResponseStream()
    Dim reader As StreamReader = New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    TextBox4.Text = responseFromServer

    Console.WriteLine(responseFromServer)

    'Clean up the streams.  
    reader.Close()
    dataStream.Close()
    response.Close()

Catch ex As Exception
    MsgBox("NO DATA FOUND / CHECK INTERNET CONNECTION")
    TextBox4.Text = ""
    TextBox3.Text = ""
    TextBox2.Text = ""
End Try

这是发布代码的输出:

THIS IS THE IMAGE OF THE OUTPUT OF THE CODE POST

我应该如何发送身体数据?

0 个答案:

没有答案