'VB.NET WebRequest with PHP POST

I am having trouble sending the post variable to received in the PHP document in my server. I tried it with the GET and it works fine. But what I notice is the POST VARIABLE doesn't receive the content I am sending. This is my code:

VB.NET WINFORM CODE

enter code here


        Dim Username = TxtUser.Text
        Dim PostData = "user_name=" & Username
        Dim request As WebRequest = WebRequest.Create("http://website.com/test.php")

        request.Method = "POST"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
        request.ContentType = "application/x-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()
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()
        MsgBox(responseFromServer)

PHP CODE

<?php
//I tried this   $user_name= 'SOMETHING';   and works fine.
$user_name= $_POST['user_name']; 
?>


Solution 1:[1]

Changing the ContentType should do the trick.

request.ContentType = "application/x-www-form-urlencoded"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prete