Я пытаюсь ПОСТАВИТЬ некоторые данные, используя VB. NET до PHP. Сценарий PHP, показанный ниже, представляет собой всего две строки для экспериментов.
$arr = array('code' => '110', 'description' => $_POST['updateDate']);
echo json_encode($arr);
В VB я пытался POST использовать WebClient
, HttpClient
и HttpWebRequest
. Только WebClient
работал как положено. Две другие вернули «Неопределенный индекс: 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""}]"