0

We have a basic print client, which listens for connections sent from the web using Ajax. There are several vairables sent over, example Jquery request for context:

$.ajax({
            url : 'http://127.0.0.1:13000',
            type : 'POST',
            data :  {
          'shipmentNumber': 'SAMPLESN998SSkLAIS',
          'labelType': 'labelTypePostageEND',
          'pdfLabel': 'pdfLabel__'
        }

This is seen as

POST / HTTP/1.1 Host: 127.0.0.1:13000 Connection: keep-alive Content-Length: 70621 Accept: / Origin: null User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Accept-Encoding: gzip, deflate, br Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 shipmentNumber=SAMPLESN998SSkLAIS&labelType=labelTypeRoyalMailEND&pdfLabel=

I need to ultimately get "labelType", "shipmentNumber" and more (note, labelType/END is the string I search for as shown below, not part of the data I want), out as variables to use and I am not sure the best way to do this. Currently I have (simplified):

        Dim port As Int32 = 13000
        Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

        server = New TcpListener(localAddr, port)

        ' Start listening for client requests.
        server.Start()

        ' Buffer for reading data
        Dim bytes(10240000) As Byte
        Dim data As String = Nothing

        ' Enter the listening loop.
        While True

            Dim client As TcpClient = server.AcceptTcpClient()

            data = Nothing

            ' Get a stream object for reading and writing
            Dim stream As NetworkStream = client.GetStream()

            Dim i As Int32

            ' Loop to receive all the data sent by the client.
            i = stream.Read(bytes, 0, bytes.Length)
            data = System.Text.Encoding.ASCII.GetString(bytes, 0, 5000)

            ' >>>> The bit that matters, split the data based on what we know will be there
            Dim searchIndex As Integer = data.IndexOf("labelType")
            Dim f As String = data.Substring(searchIndex + 1, data.IndexOf("END", searchIndex + 1) - searchIndex - 1)

            MessageBox.Show(data)

            ' >>> Response and Close connection removed to keep question clean
        End While

As you can I see I search the response for "markers" or delimiters I put in there; it feels like it will be unreliable and there should be a better way. I have seen some questions related but they don't directly address this, and I am not sure if those are even right. This may already be the best way, but without asking I don't know.

What methodology would you use to do this?

Is there a better way?

tim.baker
  • 3,109
  • 6
  • 27
  • 51

1 Answers1

1

Find the start of the parameters using shipmentNumber= as the key. Then split that using the & delimiter. Then loop thru each splitting them into parameters/values using the = delimiter.

data = System.Text.Encoding.ASCII.GetString(bytes, 0, 5000)

Dim dataStart As String = "shipmentNumber="

If data.Contains(dataStart) Then
    data = data.Substring(data.IndexOf(dataStart)).Trim
    Dim _params() As String = Split(data, "&")
    For Each p In _params
        Dim _values() As String = Split(p, "=")
        If _values.Length > 0 Then
            Dim _parameter As String = _values(0)
            Dim _value As String = ""
            If _values.Length > 1 Then
                _value = _values(1)
            End If
            Debug.Print("parameter = " & _parameter)
            Debug.Print("value = " & _value)
        End If
    Next
End If
Chase Rocker
  • 1,908
  • 2
  • 13
  • 14