hitman.star hitman.star - 1 year ago 269
Vb.net Question

How to send and recieve data from Visual Basic to PHP?

I have a vb.net application where i am sending error string to a php page to process it.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim errorString As String = "test string"
Dim request As WebRequest = WebRequest.Create("")

request.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(errorString)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
Dim response As WebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
End Sub

The responseFromServer is empty. Doesn't show the errorString.

My php page for testing looks like this:

if (isset($_POST['errorString']))
$a = $_POST['errorString'];
echo $a;
echo "ERROR: No data!";

Does anyone know what I am missing? Any help would be greatly appreciated.

Thanks in advance!

Answer Source

In your request string you have to add key value parameters like this,

Dim errorString As String = "errorString=test string"

This is because in php code you are using errorString as POST parameter to receive data for that key value, so always send data with respect to the POST/GET key you are using in PHP code.