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

问题描述:

我有一个 vb.net 应用程序,我将错误字符串发送到 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("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 是空的.不显示错误字符串.

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

我用于测试的 php 页面如下所示:

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.

提前致谢!

在你的请求字符串中,你必须像这样添加键值参数,

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

Dim errorString As String = "errorString=test string"

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

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.