2

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("http://10.0.0.1/test.php")

    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)
    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)
End Sub

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

My php page for testing looks like this:

<?php
if (isset($_POST['errorString']))
{
    $a = $_POST['errorString'];
    echo $a;
}
else
{
    echo "ERROR: No data!";
}
?>

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

Thanks in advance!

hitman.star
  • 73
  • 10
  • Is it empty response or is the request timing out (throwing an exception)?. Some access points (routers) do not allow local IPs receiving web requests without proper configuration (I have had this issue myself). Try accessing 10.0.0.1/test.php from browser (preferably another PC or mobile) and see result – Moussa Khalil Jun 12 '16 at 04:49

1 Answers1

0

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.

U.Swap
  • 1,921
  • 4
  • 23
  • 41
  • Well, if the errorString is like this array: Dim errorString As String = new String(2) {} errorString(0) = "Hello" errorString(1) = "World!" how can I send this errorString? – hitman.star Jun 13 '16 at 07:08