Visual Basic + PHP Server

后端 未结 1 1727
你的背包
你的背包 2020-12-22 02:47

I need a PHP Server to interact with my wireless sensors. But I also need that server to be controlled by a Visual Basic Application.

Some of the features I need

1条回答
  •  一整个雨季
    2020-12-22 02:56

    Well, what you want is broad and yet, there is not enough information about your PHP part.

    But I can help you with VB.NET. Here is a Class (And a Sub and an Event) that can really help.

    Some Examples first

    Simply loads a HTML code:

    Dim Page As New WEBhtml("http://www.example.com/index.php?get=something")
    While Page.IsReady = False
    End While
    If IsNothing(Page.Exception) Then 
        MsgBox(Page.GetHtml)
    Else
        MsgBox(Page.Exception.Message)
    End If
    

    Sends POST to destination (just the Dim line):

    Dim Page As New WEBhtml("http://www.example.com/index.php?get=something", {"a=alpha", "b=beta", "c=I Don't Know :D !"})
    

    Use Handling:

    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Page As New WEBhtml("http://www.e.com/i.php")
    End Sub
    Private Sub html_done(ByRef sender As WEBhtml) Handles Me.WebHtml_Done
        MsgBox("Timetook: " & sender.TimeTook / 1000 & "s")
        MsgBox("Url: " & sender.Url)
        If IsNothing(sender.Exception) Then
            MsgBox("Bandwidth: " & sender.Bytes / 1024 & "kb")
            MsgBox("HTML: " & sender.GetHtml)
        Else
            MsgBox("Error: " & sender.Exception.Message)
        End If
    End Sub
    

    See? very easy.


    Now to Start

    Follow these two steps

    First: Add System.Web Reference

    Go to Project > [Project name] Properties > Reference After that press the "Add..." button, and check 'System.Web' Then press Ok. also check it in 'Imported namespaces'

    Second: Copy this block before the 'End Class'

    Public Shared Event WebHtml_Done(ByRef sender As WEBhtml)
    Friend Shared Sub RaiseDone(ByRef wh As WEBhtml)
        RaiseEvent WebHtml_Done(wh)
    End Sub
    Public Class WEBhtml
        Private thrd As Threading.Thread
        Private Err As Exception
        Private BytesUsed As ULong = 0
        Private Time As UInteger = 0
        Private Html As String = ""
        Private _Url As String
        Private tmr As New Timer
    
        Private Sub initialize()
            tmr.Interval = 50
            AddHandler tmr.Tick, AddressOf Tick
            tmr.Enabled = True
            tmr.Start()
        End Sub
    
        Public Sub New(ByVal Url As String)
            thrd = New Threading.Thread(Sub() WEB_POST(Url))
            initialize()
            thrd.Start()
        End Sub
        Public Sub New(ByVal Url As String, ByVal PostData As String())
            thrd = New Threading.Thread(Sub() WEB_POST(Url, PostData))
            initialize()
            thrd.Start()
        End Sub
    
        Private Sub Tick(sender As Object, e As EventArgs)
            If thrd.IsAlive = False Then
                tmr.Enabled = False
                RaiseDone(Me)
            End If
        End Sub
        Private Sub WEB_POST(ByVal url As String, Optional ByVal values() As String = Nothing)
            _Url = url
            Dim data As String = ""
            Dim a, b As Integer
            b = My.Computer.Clock.TickCount
            Try
                For i = 0 To values.GetLength(0) - 1
                    a = values(i).IndexOf("=")
                    If a >= 0 Then
                        data += System.Web.HttpUtility.UrlEncode(Mid(values(i), 1, a)) & "=" & System.Web.HttpUtility.UrlEncode(Mid(values(i), a + 2))
                        If i < values.GetLength(0) - 1 Then data += "&"
                    End If
                Next
            Catch
                data = ""
            End Try
    
            Try
                Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
                request.Method = "POST"
                Dim postdata As String = data
                Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postdata)
                request.ContentType = "application/x-www-form-urlencoded"
                request.ContentLength = byteArray.Length
                request.Timeout = 100000
                Dim dataStream As IO.Stream = request.GetRequestStream()
                dataStream.Write(byteArray, 0, byteArray.Length)
                dataStream.Close()
    
                Dim response As Net.WebResponse = request.GetResponse()
                dataStream = response.GetResponseStream()
                Dim reader As New IO.StreamReader(dataStream)
                Dim responseFromServer As String = reader.ReadToEnd()
                reader.Close()
                dataStream.Close()
                response.Close()
                BytesUsed += responseFromServer.Length + byteArray.Length
                Time = My.Computer.Clock.TickCount - b
                Html = (responseFromServer)
            Catch ex As Exception
                Err = ex
                Time = My.Computer.Clock.TickCount - b
                Html = ""
            End Try
        End Sub
    
        Public ReadOnly Property Exception() As Exception
            Get
                Return Err
            End Get
        End Property
        Public ReadOnly Property TimeTook() As UInteger
            Get
                Return Time
            End Get
        End Property
        Public ReadOnly Property Bytes() As ULong
            Get
                Return BytesUsed
            End Get
        End Property
        Public ReadOnly Property GetHtml() As String
            Get
                Return Html
            End Get
        End Property
        Public ReadOnly Property IsReady() As Boolean
            Get
                Return Not thrd.IsAlive
            End Get
        End Property
        Public ReadOnly Property Url() As String
            Get
                Return _Url
            End Get
        End Property
    End Class
    

    I believe this works properly.

    Hope It Helps.

    0 讨论(0)
提交回复
热议问题