使用VB.NET将JSON POST转换为PHP

h43kikqp  于 2022-12-10  发布在  PHP
关注(0)|答案(2)|浏览(132)

我正在尝试使用VB.NET将一些数据POST到PHP中。PHP脚本(如下所示)只有两行代码供实验使用。

$arr = array('code' => '110', 'description' => $_POST['updateDate']);
echo json_encode($arr);

在VB中,我尝试使用WebClientHttpClientHttpWebRequest进行POST。只有WebClient按预期工作。其他两个返回“Undefined index:updateDate”表示POST不正确。下面是VB代码。

Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.Net.Http
Imports System.Threading.Tasks
Imports System.Windows.Forms

Public Class frmMain
    Dim hclient As HttpClient = New HttpClient

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim byteArray() As Byte
        Dim dataStream As Stream
        Dim reader As StreamReader

        Dim sresponse As String
        Dim serializer As New JavaScriptSerializer()
        Dim responseBytes As Byte()

        Dim client As New WebClient
        Dim postData As New NameValueCollection
        postData.Add("updateDate", "17-02-2020")
        responseBytes = client.UploadValues("http://localhost/srvMoveTimesARGUS/srvMoveTimesARGUS.php", postData)
        sresponse = Encoding.UTF8.GetString(responseBytes)

        Dim updateData As New List(Of field) From {
            New field With {.fieldValue = "17-02-2020", .fieldName = "updateDate"}
        }
        Dim arrayJson As String
        arrayJson = serializer.Serialize(updateData)

        '---------------------------------------------------------
        'With hclient
        '   .BaseAddress = New Uri("http://localhost/srvMoveTimesARGUS/srvMoveTimesARGUS.php")
        '   .DefaultRequestHeaders.Accept.Clear()
        '   .DefaultRequestHeaders.Accept.Add(New Headers.MediaTypeWithQualityHeaderValue("application/json"))
        'End With

        'PostAsync(arrayJson)
        '---------------------------------------------------------

        '---------------------------------------------------------
        'byteArray = Encoding.UTF8.GetBytes(arrayJson)
        'request = HttpWebRequest.Create("http://localhost/srvMoveTimesARGUS/srvMoveTimesARGUS.php")

        'request.Method = "POST"
        'request.ContentType = "application/json"
        ''request.Accept = "application/json"
        'request.ContentLength = byteArray.Length
        ''request.Expect = "application/json"

        'dataStream = request.GetRequestStream
        'dataStream.Write(byteArray, 0, byteArray.Length)
        'dataStream.Close()

        'response = request.GetResponse
        'dataStream = response.GetResponseStream()
        'reader = New StreamReader(dataStream)
        'sresponse = reader.ReadToEnd

        'reader.Close()
        'dataStream.Close()
        'response.Close()
        '---------------------------------------------------------
    End Sub

    Private Async Function PostAsync(ByVal jsonString As String) As Task

        Dim content As New Net.Http.StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")
        Dim response As Net.Http.HttpResponseMessage = Await hclient.PostAsync("", content)

        Dim result As String = Await response.Content.ReadAsStringAsync()

        MessageBox.Show(result)
    End Function
End Class

Public Class field
  Public Property fieldName As String
  Public Property fieldValue As String
End Class

保存序列化字符串的变量arrayJson取值为

"[{""fieldName"":""updateDate"",""fieldValue"":""17-02-2020""}]"
aiazj4mn

aiazj4mn1#

https://www.php.net/manual/en/reserved.variables.post.php
在请求中使用application/x-www-form-urlencoded或multipart/form-data作为HTTP Content-Type时,通过HTTP POST方法传递给当前脚本的变量的关联数组。
如果要访问原始请求主体,它将位于流php://input中,并且可以简单地使用以下命令进行访问:

$json_string = file_get_contents('php://input');
svmlkihl

svmlkihl2#

以下是我所做的和为我工作的,为那些还在寻找好答案的人:

Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports Newtonsoft.Json

Sub PostData()
Dim dbdictionary As New Dictionary(Of String, Object)

        dbdictionary.Add("ptdata", t1ds)
        dbdictionary.Add("data", t2ds)
        textstring = JsonConvert.SerializeObject(dbdictionary)

        Dim httpWebRequest = CType(WebRequest.Create("https://yourserver.com/post.php"), HttpWebRequest)
        httpWebRequest.ContentType = "text/json"
        httpWebRequest.Method = "POST"

        Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())

            streamWriter.Write(textstring)
        End Using

        Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)

        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
            Dim responseText = streamReader.ReadToEnd()
            Typethis(responseText)
        End Using

End Sub

发布到包含以下内容的PHP文件:

header("Content-Type: application/json; charset=UTF-8");
$data   = file_get_contents('php://input');
//print_r($data);
echo file_get_contents('php://input');
$Storagebackup = "backups/".strtotime("now");
$cmnt =$data  ;
$myfile = fopen($Storagebackup.".txt", "w") or die("Unable to open file!");//w means write - a means apppend n file 
$txt = $cmnt;
fwrite($myfile, $txt);
fclose($myfile);

echo "Thats all<br>";

相关问题