如何从Visual Basic发送和接收数据到PHP?

时间:2016-06-12 04:35:04

标签: php vb.net

我有一个vb.net应用程序,我将错误字符串发送到php页面来处理它。

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

responseFromServer为空。不显示errorString。

我的测试用php页面如下:

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

有谁知道我错过了什么?任何帮助将不胜感激。

提前致谢!

1 个答案:

答案 0 :(得分:0)

在您的请求字符串中,您必须添加像这样的键值参数<​​/ p>

Dim errorString As String = "errorString=test string"

这是因为在PHP代码中,您使用errorString作为POST参数来接收该键值的数据,因此始终发送与您在PHP代码中使用的POST / GET键相关的数据。