0

I'm trying to send data from a VB.NET Application to a php application, i found this code:

Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(),contentType As String, method As String) As String
    Dim req As WebRequest = WebRequest.Create(uri)
    req.ContentType = contentType
    req.Method = method
    req.ContentLength = jsonDataBytes.Length


    Dim stream = req.GetRequestStream()
    stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
    stream.Close()

    Dim response = req.GetResponse().GetResponseStream()

    Dim reader As New StreamReader(response)
    Dim res = reader.ReadToEnd()
    reader.Close()
    response.Close()

    Return res
End Function

Dim data = Encoding.UTF8.GetBytes(jsonSring)
Dim result_post = SendRequest(uri, data, "application/json", "POST")

at: source

But I can't get the posted data on php. It sends the headers, but the data no.

So, I need help to figure it out what is missing.

Community
  • 1
  • 1
guto condela
  • 9
  • 1
  • 4
  • it's writing the request as binary data. Is that what you want? Or is the PHP app expecting JSON as text? – ADyson Sep 15 '16 at 15:11
  • I need to get the data in a $_POST or $_GET, it is always empty. – guto condela Sep 16 '16 at 11:11
  • yes, I understand that. I was asking if you want it in binary or string/JSON format? Because the code you've borrowed is converting your request body to binary. If you want it as string you should remove the parts which do the binary conversion and just send the raw JSON string. – ADyson Sep 16 '16 at 11:46
  • The request Stream just accept to write bytes. – guto condela Sep 16 '16 at 15:39
  • ok fair enough. I've read the documentation and it looks like your method is basically ok in that sense. It should convert back to text again at the other end. Have you checked that (a) the JSON you are sending is valid and (b) contains the same variables that you are checking for on the PHP side? It might be worthwile posting an example of your JSON and also the PHP which is reading it. – ADyson Sep 19 '16 at 08:23

1 Answers1

0

I also had same issue and got solution from comments only. As you are passing binary data then in php you need to read binary data as raw input

To get the Raw Post Data:

<?php $postdata = file_get_contents("php://input"); ?>
Veer
  • 6,046
  • 1
  • 14
  • 12