我正在尝试使用VB.NET将一些数据POST到PHP中。PHP脚本(如下所示)只有两行代码供实验使用。
$arr = array('code' => '110', 'description' => $_POST['updateDate']);
echo json_encode($arr);
在VB中,我尝试使用WebClient
、HttpClient
和HttpWebRequest
进行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""}]"
2条答案
按热度按时间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
中,并且可以简单地使用以下命令进行访问:svmlkihl2#
以下是我所做的和为我工作的,为那些还在寻找好答案的人:
发布到包含以下内容的PHP文件: